我在MVVM项目中有一个数据网格,它有两个ComboBox列。我有一个DataTable作为ItemsSource,还有一个ObservableObject Collection作为ComboBox项的源。有一个选定项的ObservableObject实例。
虽然我可以下拉并选择框中的项目,但我的问题是,当选择一个项目时,它不会用该选择更新DataTable记录。例如,如果在记录中我想将一列设置为"0";"计算机";在组合框中,稍后查看该记录中的值,它显示为一个空单元格,而不是Computer。
绑定SelectedItem,然后用新的选择和新记录更新基础DataTable,我做错了什么?
public class AddGroupObjectTypeModel : ObservableObject
{
#region Properties
private int iD;
public int ID
{
get
{
return iD;
}
set
{
if (iD != value)
{
iD = value;
OnPropertyChanged();
}
}
}
private string objType;
public string ObjType
{
get
{
return objType;
}
set
{
if (objType != value)
{
objType = value;
OnPropertyChanged();
}
}
}
#endregion
}
private readonly ObservableCollection<AddGroupObjectTypeModel> objectTypeTerms = new()
{
new AddGroupObjectTypeModel { ID = 1, ObjType = "User" },
new AddGroupObjectTypeModel { ID = 2, ObjType = "Group" },
new AddGroupObjectTypeModel { ID = 3, ObjType = "Computer" }
};
public IEnumerable<AddGroupObjectTypeModel> ObjectTypeTerms
{
get { return objectTypeTerms; }
}
private AddGroupObjectTypeModel selectedObjectTypeTerm = new();
public AddGroupObjectTypeModel SelectedObjectTypeTerm
{
get { return selectedObjectTypeTerm; }
set { selectedObjectTypeTerm = value; OnPropertyChanged(); }
}
private DataTable objectsDisplayTable;
public DataTable ObjectsDisplayTable
{
get { return objectsDisplayTable; }
set { objectsDisplayTable = value; OnPropertyChanged(); }
}
ObjectsDisplayTable = new("Temporary Table");
ObjectsDisplayTable.Columns.Add("ObjectName", typeof(String));
ObjectsDisplayTable.Columns.Add("OType", typeof(String));
ObjectsDisplayTable.Columns.Add("ODomain", typeof(String));
<DataGrid x:Name="ObjectsTable"
AutoGenerateColumns="False"
Grid.Row="1"
CanUserResizeColumns="True"
Margin="2,0,0,0"
Style="{StaticResource ThemedDataGrid}"
BorderBrush="{x:Null}"
ItemsSource="{Binding ObjectsDisplayTable,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
CanUserAddRows="True"
SelectionMode="Single"
SelectionUnit="Cell"
>
<DataGridTemplateColumn Header="Type"
x:Name="OType"
MinWidth="200"
>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=DataContext.ObjectTypeTerms, RelativeSource={RelativeSource AncestorType=DataGrid}}"
DisplayMemberPath="ObjType"
SelectedItem="{Binding Path=DataContext.SelectedObjectTypeTerm.ObjType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType=DataGrid}}"
SelectedValue="{Binding Path=DataContext.SelectedObjectTypeTerm.ID, RelativeSource={RelativeSource AncestorType=DataGrid}}"
SelectedValuePath="ID"
SelectedIndex="0"
IsSynchronizedWithCurrentItem="false"
Style="{StaticResource ComboBoxTheme}"
/>
</DataTemplate>
您应该将ComboBox
的SelectedValue
属性绑定到要更新的DataTable
的列:
<ComboBox ItemsSource="{Binding Path=DataContext.ObjectTypeTerms, RelativeSource={RelativeSource AncestorType=DataGrid}}"
DisplayMemberPath="ObjType"
SelectedValuePath="ObjType"
SelectedValue="{Binding OType, UpdateSourceTrigger=PropertyChanged}"
SelectedIndex="0"
Style="{StaticResource ComboBoxTheme}" />
同时绑定SelectedValue
和SelectedItem
是没有意义的。