Wpf DataGridTemplateColumn组合框绑定
我有一个绑定到员工集合的DataGrid。Employee类具有类型为Country的EmployeeCountry。国家类型由CountryId和CountryName组成 我有以下XAML:Wpf DataGridTemplateColumn组合框绑定,wpf,data-binding,combobox,datagridtemplatecolumn,Wpf,Data Binding,Combobox,Datagridtemplatecolumn,我有一个绑定到员工集合的DataGrid。Employee类具有类型为Country的EmployeeCountry。国家类型由CountryId和CountryName组成 我有以下XAML: <DataGrid ItemsSource="{Binding EmployeeList}" CanUserAddRows="True"> <DataGrid.Columns> <DataGridTempla
<DataGrid ItemsSource="{Binding EmployeeList}" CanUserAddRows="True">
<DataGrid.Columns>
<DataGridTemplateColumn Header="CountryCombo2">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=DataContext.CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
DisplayMemberPath="CountryName" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
EmployeeList和CountryList是ViewModel上的ObservableCollection属性,ViewModel是包含DataGrid的窗口的DataContext。我可以用CountryList填充组合框
问题:我需要弄清楚如何设置组合框的其他属性,如SelectedValuePath、SelectedItem等,以便DataGrid的每一行在组合框中正确显示相应的EmployeeCountry。如果员工的EmployeeCountry属性为空,则组合框中不应选择任何项目
更新:即使CanUserAddRows属性设置为true,我也无法向DataGrid添加新行。我会这样做
<ComboBox SelectedValuePath="CountryName" SelectedItem="{Binding Country}" ItemsSource="{Binding Path=DataContext.CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
为了向你们展示我给你们的作品,我正在编写完整的代码
public class Employees :INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (_name == value)
return;
_name = value;
OnPropertyChanged();
}
}
private Country _employeeCountry;
public Country EmployeeCountry
{
get
{
return _employeeCountry;
}
set
{
if (_employeeCountry == value)
return;
_employeeCountry = value;
OnPropertyChanged();
}
}
}
public class Country
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (_name == value)
return;
_name = value;
}
}
}
private static ObservableCollection<Country> _countryList = new ObservableCollection<Country>(new []{ new Country{Name="US"}, new Country{Name="UK"}});
public ObservableCollection<Country> CountryList
{
get
{
return _countryList;
}
}
private ObservableCollection<Employees> _employeeList = new ObservableCollection<Employees>(new[] { new Employees { Name = "Ty", EmployeeCountry = _countryList.First() }, new Employees { Name = "Dude" } });
public ObservableCollection<Employees> EmployeeList
{
get
{
return _employeeList;
}
}
公共类员工:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
var handler=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
私有字符串\u名称;
公共字符串名
{
得到
{
返回_name;
}
设置
{
如果(_name==值)
返回;
_名称=值;
OnPropertyChanged();
}
}
私人国家(雇员国家),;
公共国家雇员国家
{
得到
{
返回-员工所在国;
}
设置
{
如果(_employeeCountry==值)
返回;
_employeeCountry=价值;
OnPropertyChanged();
}
}
}
公营国家
{
私有字符串\u名称;
公共字符串名
{
得到
{
返回_name;
}
设置
{
如果(_name==值)
返回;
_名称=值;
}
}
}
私有静态ObservableCollection_countryList=新ObservableCollection(新[]{new Country{Name=“US”},新Country{Name=“UK”});
公共可观察收集国家/地区列表
{
得到
{
返回(u countryList);;
}
}
私有ObservableCollection _employeeList=新ObservableCollection(新[]{new Employees{Name=“Ty”,EmployeeCountry=\u countryList.First()},新员工{Name=“Dude”});
公共可观察收集员工列表
{
得到
{
返回员工名单;
}
}
还有Xaml
<DataGrid ItemsSource="{Binding EmployeeList}" CanUserAddRows="True">
<DataGrid.Columns>
<DataGridTemplateColumn Header="CountryCombo2">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" DisplayMemberPath="Name"
SelectedValuePath="Name" SelectedItem="{Binding EmployeeCountry}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我可以用我的一条评论中提到的问题来解决我的问题。只需发布帮助其工作的XAML:
<DataGrid ItemsSource="{Binding EmployeeList}" CanUserAddRows="True" AutoGenerateColumns="False" Margin="0,0,0,90">
<DataGrid.Columns>
<DataGridTemplateColumn Header="CountryCombo2">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=DataContext.CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
DisplayMemberPath="CountryName"
SelectedItem="{Binding EmployeeCountry, Mode=TwoWay}"
SelectedValue="{Binding EmployeeCountry.CountryId}"
SelectedValuePath="CountryId" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我不仅发布了代码,还提供了一些我实际使用的东西。在你说它不起作用之前,先展示一下你的物体的结构。对那些试图帮助你的人要聪明,在我看来,我是目前为止唯一一个回答这个问题的人,而人们都在回避这个问题。@TTY,我完全接受你的辩护,也接受你声称我仓促行事否决了你的答案。然而,我认为我在表达我的问题时非常清楚,读者理解我的问题就足够了。我给你2分。我可以通过另一个问题解决我的问题。我会在给出答案后删除我的问题。谢谢您的帮助。@TTY,您能帮我解决我的问题更新的相关问题吗?关于向DataGrid添加新行。您的Employee类中没有默认构造函数,这就是为什么它不允许您添加行的原因。即使我添加了默认构造函数,问题仍然存在。我在DataGrid问题的一个新问题中描述了我的问题:我们能够从[这个答案][1]解决我的问题。[1]: