简体   繁体   English

避免使用WPF控件复制ItemTemplate?

[英]Avoiding ItemTemplate Duplication With WPF Controls?

I have comboboxes that all need to use a converter: 我有所有都需要使用转换器的组合框:

<ComboBox>
  <ComboBox.ItemTemplate>
    <DataTemplate>
      <ContentPresenter
            Content="{Binding Converter={StaticResource TimespanConverter}}"/>
    </DataTemplate>
  </ComboBox.ItemTemplate>
</ComboBox>

I'm currently pasting this everywhere I need it, but I am wondering if there is a way to avoid this duplication: to be able to do something like: <TimeSpanComboBox ...></...> or something similar? 我目前正在将其粘贴到需要的任何地方,但是我想知道是否有一种避免重复的方法:能够执行以下操作: <TimeSpanComboBox ...></...>或类似的方法?

You can define an implicit DataTemplate somewhere in your Application.Resources . 您可以在Application.Resources某处定义一个隐式DataTemplate ie

<DataTemplate DataType="{x:Type sys:TimeSpan}">
  <ContentPresenter
        Content="{Binding Converter={StaticResource TimespanConverter}}"/>
</DataTemplate>

You can of course also define a key and re-use it explicitly where you need it (eg ItemTemplate="{StaticResource TimeSpanTemplate}" ). 当然,您还可以定义一个密钥,并在需要的地方显式重用它(例如ItemTemplate="{StaticResource TimeSpanTemplate}" )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM