I’m practicing C# with wpf and I’m trying to use template formatting.
At this time, I have a customized combobox bind link this:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="177,28,0,0" Name="JoinedFiles_combobox" VerticalAlignment="Top" Width="164" Grid.Column="1" SelectionChanged="JoinedFiles_combobox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<Canvas Height="18">
<Image Name="ImageName" Height="16" Width="16" Canvas.Left="1" Canvas.Top="1"/>
<TextBlock Text="{Binding Name}" Canvas.Left="26" Canvas.Top="1" Width="120"/>
</Canvas>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding PictureID}" Value="0">
<Setter TargetName="ImageName" Property="Source" Value="Resources\0.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding PictureID}" Value="1">
<Setter TargetName="ImageName" Property="Source" Value="Resources\1.png"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
As I want another combobox like this in another xaml file (another window), I am trying to put this as a template in my app.xaml file. This will prevent the copying of code and simplify things.
This is what I wrote in the app.xaml file:
<Application.Resources>
<ResourceDictionary>
<ControlTemplate x:Key="CustomisedComboBox" TargetType="{x:Type ComboBox}">
<ItemTemplate>
<DataTemplate>
<Canvas Height="18">
<Image Name="ImageName" Height="16" Width="16" Canvas.Left="1" Canvas.Top="1"/>
<TextBlock Text="{Binding Name}" Canvas.Left="26" Canvas.Top="1" Width="120"/>
</Canvas>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding PictureID}" Value="0">
<Setter TargetName="ImageName" Property="Source" Value="Resources\0.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding PictureID}" Value="1">
<Setter TargetName="ImageName" Property="Source" Value="Resources\1.png"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemTemplate>
</ControlTemplate>
</ResourceDictionary>
</Application.Resources>
The problem is that it can’t find ItemTemplate in my control template. How can I give it that item template?
I think you need a
Stylehere, notControlTemplate:ControlTemplatedefines how control is rendered whileStyledefines control property values.