Felix Brüll
Felix Brüll

Reputation: 377

WPF use static resource in DataTemplate

I have a user control defines in my Application.Resources and want to use it as a DataTemplate for a ComboBox.

The user control:

<TextBlock 
   x:Key="ListItemView"
   Text="{Binding Name}"
   ToolTip="{Binding ToolTip}"/>

The Combox Box in my Window:

<ComboBox 
   ItemsSource="{Binding ComboBoxItems}"
   SelectedItem="{Binding SelectedComboBoxItem}">
   <ComboBox.ItemTemplate>
      <DataTemplate>
         <!-- TODO how to use StaticResource ListItemView in here? -->
      </DataTemplate>
   </ComboBox.ItemTemplate>
</ComboBox>

Upvotes: 0

Views: 1051

Answers (1)

Pawleshhh
Pawleshhh

Reputation: 44

  1. You must put your user control into DataTemplate

    <DataTemplate x:Key="ListItemView">
        <TextBlock Text="{Binding Name}"
                   ToolTip="{Binding ToolTip}"/>
    </DataTemplate>
    
  2. Use your data template

    <ComboBox ItemsSource="{Binding ComboBoxItems}" 
              SelectedItem="{Binding SelectedComboBoxItem}"
              ItemTemplate="{StaticResource ListItemView}">
    </ComboBox>
    

Upvotes: 2

Related Questions