【问题标题】:Reusing DataTemplates重用数据模板
【发布时间】:2016-07-26 11:55:04
【问题描述】:

在我的 UI 中有几个 GroupBoxes,我正在修改他们的 HeaderTemplate 以在文本标题之外添加图标。这是我的模板:

<DataTemplate>
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Image Source="/Resources/IDCard.png" />
    <TextBlock Grid.Column="1" Text="{Binding}" VerticalAlignment="Center" />
  </Grid>
</DataTemplate>

这行得通,但是由于我有多个GroupBoxes,我正在考虑一种在WindowResources 中定义此模板的方法,然后在每个GroupBox 中引用它。如何在上述模板中使用ContentPresenter(或其他任何内容)代替&lt;Image&gt; 控件,然后分别在每个GroupBox 中提供实际内容(图标)?

注意:我正在寻找仅限 XAML 的解决方案。

【问题讨论】:

    标签: wpf xaml datatemplate contentpresenter


    【解决方案1】:

    您可以尝试使用DynamicResource 查找(沿着this example 的行)。

    【讨论】:

    • 太棒了。非常感谢。
    猜你喜欢
    • 2011-10-21
    • 2023-03-12
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多