WPF MVVM Datagid with ComboBox column set ComboBox的背景与替代颜色

xyhw6mcr  于 2023-05-08  发布在  其他
关注(0)|答案(1)|浏览(122)

我已经编辑了ComboBoxStyle,将ComboBoxToggleButton'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>

如何做到这一点?

x6h2sr28

x6h2sr281#

用途:

<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Value="1">
     <Setter Property="Background" Value="#FFFFFFCC"/>
</DataTrigger>

相关问题