My ComboBox has a DataTemplate. I refer to this Can I use a different Template for the selected item in a WPF ComboBox than for the items in the dropdown part? because my request is similar to it:
<ComboBox x:Name="MyComboBox" ItemsSource="{Binding Items}"
Style="{StaticResource ComboBoxStyle}"
Template="{StaticResource ComboBoxControlTemplate}"
ItemTemplateSelector="{UI:ComboBoxTemplateSelector
SelectedItemTemplate={StaticResource ComboBoxSelectedItemDataTemplate},
DropdownItemsTemplate={StaticResource ComboBoxDataTemplate}}"/>
It works well but I got another request: To set the ComboBox
's ToggleButton
's Background
based on SelectedItem
. So I right click this ComboBox
in the designer page, choose
Edit Template -> Edit a Copy
to generate an automatic template. In the automatically generated template I get:
<ControlTemplate x:Key="ComboBoxControlTemplate" TargetType="{x:Type ComboBox}">
<ToggleButton>
<Border x:Name="splitBorder" Background="{TemplateBinding Background}" ...>
<Path x:Name="Arrow" .../>
</Border>
</ToggleButton>
</ControlTemplate>
So what I want to change in the above code is Background="{TemplateBinding Background}"
. I want the Background
to be based on an ItemSource ViewModel property called SelectedName
just like in DataTemplate
I have something like:
<DataTemplate x:Key="ComboBoxSelectedItemDataTemplate">
<Grid>
...
<TextBlock Background="{Binding Converter="{StaticResource StringToColorConverter}", Path="SelectedName"}"/>
</Grid>
</DataTemplate>
I searched quite a few but not sure what I should do to achieve this. Please help. Thanks.