我已经编辑了ComboBox
Style
,将ComboBox
ToggleButton's Background
绑定到ComboBox Background
属性(显然是唯一的方法)。
...
<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>
...
下面的XAML
代码可以工作了:
<ComboBox Background="Yellow" />
现在我有一个ComboBox
列的Datagrid
,我想将替代行颜色绑定到ComboBox Background
属性:
<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>
如何做到这一点?
1条答案
按热度按时间x6h2sr281#
用途: