是否可以使用元素绑定和DataGridComboBoxColumns选定项目?DataGridComboBoxColumn上的元素绑定
我有一些对象MyObject,它包含ObservableCollection类型的属性“ListOfSomeTypes”。 SomeType实现INotifyPropertyChanged。
SomeType的列表应显示在DataGrid中。对于“ListOfSomeTypes”我正在寻找解决方案来执行以下操作:DataGrid有两列。一个是具有固定列表整数的DataGridComboBoxColumn,例如{0,1,2,3}。该列的ItemsSource通过绑定(属性“ListOfIndices”,不属于MyObject)设置。第二列应在所选索引位置(来自另一列)显示“ListOfSomeTypes”的内容。 所以我想我可以使用MultiConverter来检查两者的值并选择正确的显示值。问题是,虽然组合框显示我的索引列表中的第一项,但应该是索引的值从不设置(null)。
由于只需要显示索引,我不想在MyObject类中拥有“SelectedIndex”属性。是否可以通过元素绑定访问组合框的选定值?哪些属性需要使用(因为SelectedValueBinding似乎是错误的)?或者,还有更好的方法?
这是我的代码:
<DataGridCheckBoxColumn Header="SomeType">
<DataGridCheckBoxColumn.Binding>
<MultiBinding Converter="converters:SomeTypeCodeToBoolMultiConverter}">
<Binding Path="ListOfSomeTypes" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
<Binding ElementName="dgcbcSelectedIndex" Path="SelectedValueBinding"
UpdateSourceTrigger="PropertyChanged"/>
</MultiBinding>
</DataGridCheckBoxColumn.Binding>
</DataGridCheckBoxColumn>
<DataGridComboBoxColumn x:Name="dgcbcSelectedIndex" Header="Indices">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=Data.ListOfIndices, Source={StaticResource proxy}, UpdateSourceTrigger=PropertyChanged}" />
<Setter Property="IsSynchronizedWithCurrentItem" Value="True"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=Data.ListOfIndices, Source={StaticResource proxy}, UpdateSourceTrigger=PropertyChanged}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
转换器
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null || values.Length != 2)
return null;
var listOfSomeTypes= (ObservableCollection<SomeType>) values[0];
if (listOfSomeTypes!= null)
{
int selectedIndex = 0;
if (values[1] != null)
{
try { selectedIndex = (int)values[1]; }
catch (Exception) { }
}
if (listOfSomeTypes.Count > selectedIndex)
{
var someType= listOfSomeTypes[selectedIndex ];
return someType == TypeA;
}
}
return null;
}
感谢您的帮助!
所以,你的意思是我必须在我的“MyObject”类中有某种“SelectedIndex”属性? – tabina
是的,确切地说。 ... – mm8