[英]Datagrid combobox databinding updating MVVM
我在理解如何使用嵌入在数据网格中的组合框时遇到问题。 例如,我有一个绑定到类GridModel的数据网格:
public partial class MainWindow : Window
{
public GridModel gridModel { get; set; }
public MainWindow()
{
InitializeComponent();
gridModel = new GridModel();
dgCustomers.DataContext = gridModel;
}
}
GridModel类为:
public class GridModel : ViewModelBase
{
public ObservableCollection<Record> customers { get; set; }
public List<Country> countries { get; set; }
public GridModel()
{
customers = new ObservableCollection<Record>();
customers.Add(new Record { name = "Alan", phone = "123" });
customers.Add(new Record { name = "Bert", phone = "234" });
countries = new List<Country> { new Country { id = 1, name = "England", code = 44 }, new Country { id = 2, name = "Germany", code = 49 } };
}
}
XAML是:
<Window x:Class="Customer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid x:Name="dgCustomers" AutoGenerateColumns="False" ItemsSource="{Binding customers}">
<DataGrid.Resources>
<DataTemplate x:Key="dgCombobox">
<ComboBox x:Name="cmbCountry"
ItemsSource="{Binding countries}"
DisplayMemberPath="{Binding name}"
SelectedValue="{Binding customers.countryCode, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedValuePath="{Binding code}">
</ComboBox>
</DataTemplate>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding name}"/>
<DataGridTemplateColumn Header="Country" CellTemplate="{StaticResource dgCombobox}" />
<DataGridTextColumn Header="Phone" Binding="{Binding phone}" ></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
第二列中的组合框应该由模型的countrys属性填充,该属性是Country类的列表:
public class Country : ViewModelBase
{
private string _name;
public string name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("name");
}
}
private int _id;
public int id
{
get { return _id; }
set
{
_id = value;
OnPropertyChanged("id");
}
}
private int _code;
public int code
{
get { return _code; }
set
{
_code = value;
OnPropertyChanged("code");
}
}
}
组合框应显示国家名称。 从类记录的客户ObservableCollection填充数据网格:
public class Record : ViewModelBase
{
private string _name;
public string name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("name");
}
}
private string _phone;
public string phone
{
get { return _phone; }
set
{
_phone = value;
OnPropertyChanged("phone");
}
}
private int _countryCode;
public int countryCode
{
get { return _countryCode; }
set
{
_countryCode = value;
OnPropertyChanged("countryCode");
}
}
}
为了完整起见,这是ViewModelBase类:
public class ViewModelBase : INotifyPropertyChanged
{
public ViewModelBase()
{
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
我想要的是用户能够从每行的组合框中为客户选择一个国家/地区,然后该行的记录将其countryCode属性设置为所选国家/地区的code属性。 我使用了datagridtemplatecolumn,以便组合框显示在网格上。 任何帮助表示赞赏,我已经为此努力了一个多月,并且开始感到非常愚蠢。
第二列中的组合框应该由模型的countrys属性填充,
我为此重新安排了您的datagrid xaml
<Grid>
<DataGrid ItemsSource="{Binding customers}"
AutoGenerateColumns="False" SelectedItem="{Binding SelectedRow,Mode=TwoWay}"
CanUserAddRows="False" Grid.Row="1">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding name}" Header="Name"
Width="*"/>
<DataGridTemplateColumn Width="Auto" Header="Country">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding DataContext.countries,
RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
DisplayMemberPath="name" SelectedValuePath="name" Margin="5"
SelectedItem="{Binding DataContext.SelectedCountry,RelativeSource={RelativeSource AncestorType={x:Type Window}},Mode=TwoWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding phone}" Header="Phone"
Width="*"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
我想要的是用户能够从每行的组合框中为客户选择一个国家/地区,然后该行的记录将其countryCode属性设置为所选国家/地区的code属性。
在您的视图模型中,删除dgCustomers.DataContext = gridModel;
在MVVM中,永远不要访问后面代码中的控件名称,而用this.DataContext = gridModel;
还添加
private Country _selectedCountry;
public Country SelectedCountry
{
get
{
return _selectedCountry;
}
set
{
_selectedCountry = value;
_selectedRow.countryCode = _selectedCountry.code;
OnPropertyChanged("SelectedRow");
}
}
private Record _selectedRow;
public Record SelectedRow
{
get {
return _selectedRow;
}
set
{
_selectedRow = value;
OnPropertyChanged("SelectedRow");
}
}
更改列表框中的项目时,所选行的值将更新。您可以放置调试器并检查值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.