【问题标题】:Use databinding with the ToolBarTray control in WPF在 WPF 中将数据绑定与 ToolBarTray 控件一起使用
【发布时间】:2014-02-04 15:11:19
【问题描述】:

我正在开发 WPF/PRISM 应用程序。我首先按照该示例使用数据绑定创建了一个动态菜单:http://www.koaxkoaxkoax.com/ribbit/2010/09/creating-dynamic-menus-in-wpf.html 它使用 HierarchicalDataTemplate,这似乎是一个不错的解决方案。

我的目标是对工具栏使用相同的概念,但遗憾的是 ToolBarTray 控件没有 ItemsSource 来在其中动态生成工具栏控件。

我是 WPF 的新手,我似乎找不到一个好的解决方案来使用 数据绑定 在 ToolBarTray 中创建工具栏。有人有解决办法吗?

HierarchicalDataTemplate可以吗?

谢谢

【问题讨论】:

    标签: c# wpf data-binding


    【解决方案1】:

    虽然 ToolBarTray 不包含 ItemsSource 属性,但它确实包含您可以绑定到的 ToolBars 属性。然后,每个 ToolBar 都有一个 ItemsSource 属性,您也可以绑定到该属性。

    所以你的绑定看起来像这样:

    <ToolBarTray ToolBars="{Binding MyToolBarsProperty}" />
    

    然后您可以开始在工具栏上使用 HierarchicalDataTemplates:

    <HierarchicalDataTemplate.ItemContainerStyle>
        <Style TargetType="Button">
            <Setter Property="Command" Value="{Binding Command}"/>
        </Style>
    </HierarchicalDataTemplate.ItemContainerStyle>
    

    【讨论】:

    猜你喜欢
    • 2021-05-28
    • 2010-12-01
    • 1970-01-01
    • 2011-04-01
    • 2018-05-23
    • 2017-05-10
    • 2012-05-15
    • 1970-01-01
    相关资源
    最近更新 更多