jmasterx
jmasterx

Reputation: 54183

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?

Upvotes: 0

Views: 68

Answers (1)

brunnerh
brunnerh

Reputation: 185445

You can define an implicit DataTemplate somewhere in your Application.Resources. i.e.

<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 (e.g. ItemTemplate="{StaticResource TimeSpanTemplate}").

Upvotes: 3

Related Questions