我有一个绑定到一组员工的DataGrid。Employee类具有类型为Country的EmployeeCountry。国家类型由CountryId和CountryName组成。
我有以下XAML:
<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。我能够用ComboBox填充CountryList。
问题:我需要弄清楚如何设置ComboBox的其他属性,如SelectedValuePath、SelectedItem等,以便DataGrid的每一行都正确地在ComboBox中显示适当的EmployeeCountry。如果员工的EmployeeCountry属性为NULL,则ComboBox不应选择任何项。
更新:即使CanUserAddRows属性设置为true,也无法向DataGrid添加新行。
发布于 2014-03-12 15:33:18
我能够用我在其中一个评论中提到的问题来解决我的关切。只是发布了帮助它正常工作的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>发布于 2014-03-11 10:57:12
我会做这样的事
<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;
}
}和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>https://stackoverflow.com/questions/22323130
复制相似问题