C# 基于标题值使用ComboBox设置DataGridCell样式

C# 基于标题值使用ComboBox设置DataGridCell样式,c#,wpf,datagrid,combobox,C#,Wpf,Datagrid,Combobox,我们有一个用户控件,它显示用户可以查看的可用SQL表的组合框。每个表包含不同的数据、列数等,因此网格是自动生成的。DataView绑定到网格,当用户从combobox中选择不同的表时,通过SQL调用重新填充网格 现在,我有一个请求,即对于其中一个表,名为SourceCodeType的列将显示为仅包含几个值(L1、L2、L3)的组合框 我有以下样式(和VM属性TransposeCodeTypeComboBoxValues),它显示带有值列表的ConboBox: <Window.Resourc

我们有一个用户控件,它显示用户可以查看的可用SQL表的组合框。每个表包含不同的数据、列数等,因此网格是自动生成的。DataView绑定到网格,当用户从combobox中选择不同的表时,通过SQL调用重新填充网格

现在,我有一个请求,即对于其中一个表,名为
SourceCodeType
的列将显示为仅包含几个值(L1、L2、L3)的组合框

我有以下样式(和VM属性TransposeCodeTypeComboBoxValues),它显示带有值列表的ConboBox:

<Window.Resources>

    <ResourceDictionary>

            <CollectionViewSource x:Key="TransposeCodeTypeCollection"
                                  Source="{Binding RelativeSource={RelativeSource FindAncestor, 
                                            AncestorType={x:Type Window}}, Path=DataContext.TransposeCodeTypeComboBoxValues}" />

            <Style TargetType="{x:Type DataGridCell}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Column.Header, Mode=OneWay,

                                            Converter={StaticResource StringComparisonToBooleanConverter}, ConverterParameter='SourceCodeType'}" Value="True">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <ComboBox Name="TransposeCodeComboBox"
                                              IsReadOnly="True"
                                              ItemsSource="{Binding Source={StaticResource TransposeCodeTypeCollection}}"
                                              Width="150" />
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>

    </ResourceDictionary>
</Window.Resources>

...

<StackPanel>

                <DataGrid Name="AdjustmentTablesDataGrid" 
                          ItemsSource="{Binding AdjustmentTableDataView, UpdateSourceTrigger=PropertyChanged}"
                          SelectedItem="{Binding SelectedProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                          Margin="0,20,0,10"
                          AutoGenerateColumns="True"
                          ColumnWidth="*"
                          CanUserAddRows="False"
                          MaxHeight="500"
                          EnableRowVirtualization="True"
                          IsEnabled="True">
我仍然存在的问题是,这似乎绑定到最后一行的SourceCodeType,如果我更改一个组合框,它们都会更改为新选择的值


如何使样式正确绑定到每一行数据?

通过另一篇关于不同主题的帖子,我找到了答案:

        <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Column.Header, Mode=OneWay, 
                                        Converter={StaticResource StringComparisonToBooleanConverter}, ConverterParameter=DestinationCodeType}" Value="True">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                               <ComboBox Name="TransposeCodeComboBox"
                                         IsReadOnly="True" 
                                         ItemsSource="{Binding Source={StaticResource TransposeCodeTypeCollection}}"
                                         SelectedValue="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=DataContext.DestinationCodeType, Mode=TwoWay}"
                                         IsSynchronizedWithCurrentItem="False"
                                         Margin="0"
                                         Width="Auto"
                                         Height="Auto" />

                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>


我错过的一件大事是通过另一篇关于不同主题的帖子,我找到了以下问题的答案:

        <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Column.Header, Mode=OneWay, 
                                        Converter={StaticResource StringComparisonToBooleanConverter}, ConverterParameter=DestinationCodeType}" Value="True">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                               <ComboBox Name="TransposeCodeComboBox"
                                         IsReadOnly="True" 
                                         ItemsSource="{Binding Source={StaticResource TransposeCodeTypeCollection}}"
                                         SelectedValue="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=DataContext.DestinationCodeType, Mode=TwoWay}"
                                         IsSynchronizedWithCurrentItem="False"
                                         Margin="0"
                                         Width="Auto"
                                         Height="Auto" />

                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

我缺少的最重要的东西是
IsSynchronizedWithCurrentItem=“False”