I have edited the ComboBox
Style
to bind the ComboBox
ToggleButton's Background
to the ComboBox Background
property (the only way to do it apparently).
...
<Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
...
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border x:Name="templateRoot" BorderBrush="{StaticResource ComboBox.Static.Border}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> <= BINDING HERE
<Border x:Name="splitBorder" BorderBrush="Transparent" BorderThickness="1" HorizontalAlignment="Right"
Margin="0" SnapsToDevicePixels="true"
Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
<Path x:Name="arrow"
Data="F1 M 0,0 L 2.667,2.66665 L 5.3334,0 L 5.3334,-1.78168 L 2.6667,0.88501 L0,-1.78168 L0,0 Z"
Fill="{StaticResource ComboBox.Static.Glyph}" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center"/>
</Border>
</Border>
<ControlTemplate.Triggers>
...
So this XAML
code now works:
<ComboBox Background="Yellow" />
Now I have Datagrid
with a ComboBox
column, I would like to bind the alternative row color to the ComboBox Background
property:
<DataGrid AutoGenerateColumns="False" Name="datagrid1"
Height="auto"
ItemsSource="{Binding SourceList}"
SelectedItem="{Binding SelectedSource}"
BorderThickness="1"
AlternatingRowBackground="#FFFFFFCC"
HorizontalGridLinesBrush="#FFA0A0A0"
VerticalGridLinesBrush="#FFA0A0A0"
SelectionUnit="FullRow"
HeadersVisibility="Column"
GridLinesVisibility="Horizontal"
CanUserResizeRows="False"
VerticalContentAlignment="Center"
BorderBrush="DarkGray"
HorizontalAlignment="Stretch">
<DataGrid.Columns>
<DataGridTextColumn Header="NameText" Binding="{Binding NameText}" Width="*"/>
<DataGridTemplateColumn Width = "*" Header = "Names">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox VerticalContentAlignment = "Center"
ItemsSource = "{Binding DataContext.NamesList, RelativeSource ={RelativeSource Findancestor, AncestorType = { x:Type Window}}}"
SelectedItem="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Name">
<ComboBox.Style>
<Style TargetType="ComboBox" BasedOn="{StaticResource {x:Type ComboBox}}">
<Setter Property="Background" Value="Yellow"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ItemsControl.AlternationIndex, ElementName=datagrid1}" Value="1">
<Setter Property="Background" Value="#FFFFFFCC"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
How to achieve that?
Use:
<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Value="1">
<Setter Property="Background" Value="#FFFFFFCC"/>
</DataTrigger>