我在理解如何使用嵌入数据网格中的组合框时遇到了问题。例如,我有一个绑定到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>
第二列中的组合框应该从模型的countries属性填充,这是类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属性设置为所选国家/地区的代码属性。我使用了datagridtemplatecolumn,以便在网格上显示组合框。任何帮助表示赞赏,我已经对这个问题感到震惊了一个多月,我开始感到非常愚蠢。