如何填充 DataGridComboBoxColumn?
How to populate a DataGridComboBoxColumn?
我有一个 DataGrid,其中一列是 DataGridComboBoxColumn:
<DataGrid AutoGenerateColumns="False" CanUserAddRows="True">
<DataGrid.Columns>
<DataGridTextColumn Header="User Name" Width="Auto" Binding="{Binding UName}"/>
<DataGridComboBoxColumn Header="Country" Width="Auto" ????/>
</DataGrid.Columns>
</DataGrid>
我有一个国家class:
public class Country{
public string name {get; set;}
public string des {get; set;}
public Country() {}
public Country(string n, string d) {
this.name = n;
this.des = d;
}
}
它们有名称和描述,我希望(多个国家/地区对象的)名称填充组合框(以及每个组合框选项的工具提示),我该怎么做?
注意:稍后我想让用户能够添加一个新的国家,所以我希望组合框也能改变(包括新的国家),这意味着解决方案需要允许.
注2:我没有使用MVVM模式。编辑:意思是它不是重复的。
解决方法:
1. XAML:
<DataGrid x:Name="DataGrid" AutoGenerateColumns="False" CanUserAddRows="True">
<DataGrid.Columns>
<DataGridTextColumn Header="User UName" Width="Auto" Binding="{Binding UName}"/>
<DataGridComboBoxColumn x:Name="ComboBoxColumn" Header="Country" Width="Auto" DisplayMemberPath="name" SelectedItemBinding="{Binding CountryData}"/>
</DataGrid.Columns>
</DataGrid>
2。后面的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitData();
ComboBoxColumn.ItemsSource = CountriesCollection;
DataGrid.ItemsSource = UsersCollection;
}
private void InitData()
{
UsersCollection = new ObservableCollection<UserData>(new List<UserData>
{
new UserData
{
UName = "Greg",
},
new UserData
{
UName = "Joe",
},
new UserData
{
UName = "Iv",
}
});
CountriesCollection = new ObservableCollection<Country>(new List<Country>
{
new Country("Ger", "1500"),
new Country("Fra", "1500"),
new Country("Ru", "1500"),
new Country("Bel", "1500"),
});
}
public ObservableCollection<Country> CountriesCollection { get; set; }
public ObservableCollection<UserData> UsersCollection { get; set; }
}
3。用户模型:
public class UserData
{
public string UName { get; set; }
public object CountryData { get; set; }
}
4。工具提示支持:用下一个 xaml 代码替换所需的组合框列:
<DataGridComboBoxColumn x:Name="ComboBoxColumn" Header="Country" DisplayMemberPath="CountryName"
ItemsSource="{StaticResource CountriesArray}" Width="Auto"
SelectedItemBinding="{Binding CountryData, UpdateSourceTrigger=PropertyChanged}">
<DataGridComboBoxColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="ToolTip">
<Setter.Value>
<ContentControl Content="{Binding }">
<ContentControl.ContentTemplate>
<DataTemplate DataType="{x:Type soDataGridProjectsHelpAttempt:UserData}">
<DataTemplate.Resources>
<system:String x:Key="NoAnyEntriesKey">
No any entry presented
</system:String>
</DataTemplate.Resources>
<TextBlock Text="{Binding CountryData.Description, FallbackValue={StaticResource NoAnyEntriesKey}}"></TextBlock>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
</Setter.Value>
</Setter>
</Style>
</DataGridComboBoxColumn.CellStyle>
</DataGridComboBoxColumn>
并考虑到您需要使用描述 属性 扩展国家/地区模型。
问候,
我有一个 DataGrid,其中一列是 DataGridComboBoxColumn:
<DataGrid AutoGenerateColumns="False" CanUserAddRows="True">
<DataGrid.Columns>
<DataGridTextColumn Header="User Name" Width="Auto" Binding="{Binding UName}"/>
<DataGridComboBoxColumn Header="Country" Width="Auto" ????/>
</DataGrid.Columns>
</DataGrid>
我有一个国家class:
public class Country{
public string name {get; set;}
public string des {get; set;}
public Country() {}
public Country(string n, string d) {
this.name = n;
this.des = d;
}
}
它们有名称和描述,我希望(多个国家/地区对象的)名称填充组合框(以及每个组合框选项的工具提示),我该怎么做?
注意:稍后我想让用户能够添加一个新的国家,所以我希望组合框也能改变(包括新的国家),这意味着解决方案需要允许.
注2:我没有使用MVVM模式。编辑:意思是它不是重复的。
解决方法: 1. XAML:
<DataGrid x:Name="DataGrid" AutoGenerateColumns="False" CanUserAddRows="True">
<DataGrid.Columns>
<DataGridTextColumn Header="User UName" Width="Auto" Binding="{Binding UName}"/>
<DataGridComboBoxColumn x:Name="ComboBoxColumn" Header="Country" Width="Auto" DisplayMemberPath="name" SelectedItemBinding="{Binding CountryData}"/>
</DataGrid.Columns>
</DataGrid>
2。后面的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitData();
ComboBoxColumn.ItemsSource = CountriesCollection;
DataGrid.ItemsSource = UsersCollection;
}
private void InitData()
{
UsersCollection = new ObservableCollection<UserData>(new List<UserData>
{
new UserData
{
UName = "Greg",
},
new UserData
{
UName = "Joe",
},
new UserData
{
UName = "Iv",
}
});
CountriesCollection = new ObservableCollection<Country>(new List<Country>
{
new Country("Ger", "1500"),
new Country("Fra", "1500"),
new Country("Ru", "1500"),
new Country("Bel", "1500"),
});
}
public ObservableCollection<Country> CountriesCollection { get; set; }
public ObservableCollection<UserData> UsersCollection { get; set; }
}
3。用户模型:
public class UserData
{
public string UName { get; set; }
public object CountryData { get; set; }
}
4。工具提示支持:用下一个 xaml 代码替换所需的组合框列:
<DataGridComboBoxColumn x:Name="ComboBoxColumn" Header="Country" DisplayMemberPath="CountryName"
ItemsSource="{StaticResource CountriesArray}" Width="Auto"
SelectedItemBinding="{Binding CountryData, UpdateSourceTrigger=PropertyChanged}">
<DataGridComboBoxColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="ToolTip">
<Setter.Value>
<ContentControl Content="{Binding }">
<ContentControl.ContentTemplate>
<DataTemplate DataType="{x:Type soDataGridProjectsHelpAttempt:UserData}">
<DataTemplate.Resources>
<system:String x:Key="NoAnyEntriesKey">
No any entry presented
</system:String>
</DataTemplate.Resources>
<TextBlock Text="{Binding CountryData.Description, FallbackValue={StaticResource NoAnyEntriesKey}}"></TextBlock>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
</Setter.Value>
</Setter>
</Style>
</DataGridComboBoxColumn.CellStyle>
</DataGridComboBoxColumn>
并考虑到您需要使用描述 属性 扩展国家/地区模型。
问候,