样式DataGridCell与ComboBox基于头值
本文关键字:于头值 ComboBox DataGridCell 样式 | 更新日期: 2023-09-27 17:53:51
我们有一个用户控件,它显示用户可以查看的可用SQL表的组合框。每个表包含不同的数据、列数等,因此网格是自动生成的。当用户从组合框中选择一个不同的表时,通过SQL调用将DataView绑定到网格上。
我现在有一个请求,仅为其中一个表,名为SourceCodeType
的列显示为仅包含几个值(L1, L2, L3)的组合框。
我有以下样式(和虚拟机属性transpsecodetypecomboboxvalues),显示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到正确的组合框值时,表被加载/显示。
编辑
我发现我可以绑定到SourceCodeType
使用这个组合框的样式:
<ComboBox Name="TransposeCodeComboBox"
IsReadOnly="True"
ItemsSource="{Binding Source={StaticResource TransposeCodeTypeCollection}}"
SelectedItem="{Binding SourceCodeType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Width="150" />
我仍然存在的问题是,这似乎是绑定到最后一行的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>
我错过的大东西是IsSynchronizedWithCurrentItem="False"