【问题标题】:Stretching Toolbar in a toolbartray codebehind and xaml在工具栏代码隐藏和 xaml 中拉伸工具栏
【发布时间】:2012-03-27 00:03:36
【问题描述】:

我需要cs中的以下xaml代码sn-p(文件隐藏代码)

<ToolBarTray Width="450" IsLocked="True" >
<ToolBar Width="{Binding ActualWidth,     
RelativeSource={RelativeSource FindAncestor,     
AncestorType={x:Type ToolBarTray}}}">
<Button>B1</Button>
<Button>B2</Button>
</ToolBar>
</ToolBarTray>

【问题讨论】:

    标签: wpf xaml binding code-behind


    【解决方案1】:

    如果您真的打算在代码隐藏中使用此代码,那么下面的 sn-p 应该没问题。但是,如果您想从代码创建 DataTemplate,它将不起作用。在这种情况下,您需要使用 FrameworkElementFactory 派生类型,而不是 FrameoworkElement 派生类型。

    public ToolBarTray CreatetoolBarTray()
        {
            var tbt = new ToolBarTray
                          {
                              Width = 450.0,
                              IsLocked = true
                          };
            var tb = new ToolBar();
            var b = new Binding
                        {
                            Path = new PropertyPath("ActualWidth"),
                            Source = new RelativeSource(RelativeSourceMode.FindAncestor, typeof (ToolBarTray), 1),
                        };
            tb.SetBinding(WidthProperty, b);
    
            tb.Items.Add(new Button() {Content = "b1"});
            tb.Items.Add(new Button() {Content = "b2"});
    
            tbt.ToolBars.Add(tb);
    
            return tbt;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多