【转】WPF DataGridComboBoxColumn使用
若要填充下拉列表,请首先使用下列选项之一设置 ComboBox 的 ItemsSource 属性。
静态资源。
x:Static 代码实体。
ComboBoxItem 类型的内联集合。
实现效果如下:
如需使用非静态资源,则需要使用DataGridComboBoxColumn的EditingElementStyle样式修改编辑样式,通过ElementStyle修改正常显示(非编辑状态)样式。
主要XAML代码如下:
<DataGrid x:Name="grd" ItemsSource="{Binding StuList3}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding No}" Header="学号"/> <DataGridTextColumn Binding="{Binding Name}" Header="姓名"/> <!--使用普通List集合--> <DataGridComboBoxColumn x:Name="cmb" Header="性别"> <DataGridComboBoxColumn.EditingElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding Path=DataContext.SexList,ElementName=grd}" /> <Setter Property="DisplayMemberPath" Value="Name" /> <Setter Property="SelectedValuePath" Value="Name" /> <Setter Property="SelectedValue" Value="{Binding Sex,UpdateSourceTrigger=PropertyChanged}" /> </Style> </DataGridComboBoxColumn.EditingElementStyle> <DataGridComboBoxColumn.ElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding Path=DataContext.SexList,ElementName=grd}" /> <Setter Property="DisplayMemberPath" Value="Name" /> <Setter Property="SelectedValuePath" Value="Name" /> <Setter Property="SelectedValue" Value="{Binding Sex}" /> </Style> </DataGridComboBoxColumn.ElementStyle> </DataGridComboBoxColumn> <!--使用静态资源--> <DataGridComboBoxColumn Header="性别(静态资源)" ItemsSource="{Binding Source={StaticResource myEnum}}" TextBinding="{Binding Sex}" SelectedItemBinding="{Binding Sex,UpdateSourceTrigger=PropertyChanged}"> </DataGridComboBoxColumn> <!--使用x:Static扩展标记--> <DataGridComboBoxColumn Header="性别(x:Static)" ItemsSource="{x:Static local:ViewModel.SexList2}" TextBinding="{Binding Sex}" SelectedItemBinding="{Binding Sex,UpdateSourceTrigger=PropertyChanged}"> </DataGridComboBoxColumn> <!--使用内联集合--> <DataGridComboBoxColumn Header="性别(内联集合)" TextBinding="{Binding Sex}" SelectedItemBinding="{Binding Sex,UpdateSourceTrigger=PropertyChanged}"> <DataGridComboBoxColumn.ItemsSource> <col:ArrayList> <sys:String>男</sys:String> <sys:String>女</sys:String> </col:ArrayList> </DataGridComboBoxColumn.ItemsSource> </DataGridComboBoxColumn> </DataGrid.Columns> </DataGrid>
原文地址:https://blog.csdn.net/rabbitsoft_1987/article/details/22896073
根据上述文章,我试了一下简化,可以实现效果
前端:(DisplayMemberPath="Name" 这里是显示 Element的 名字)
<DataGrid Name="grdBeam" ItemsSource="{Binding BeamCollection}" Grid.ColumnSpan="3" CanUserAddRows="False" AutoGenerateColumns="False" Background="LightGray"> <DataGrid.Columns> <DataGridComboBoxColumn Header="材质" SelectedItemBinding="{Binding BeamMaterial}" DisplayMemberPath="Name" Width="1*"> <DataGridComboBoxColumn.EditingElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding Path=DataContext.MaterialItems,ElementName=grdBeam}" /> </Style> </DataGridComboBoxColumn.EditingElementStyle> <DataGridComboBoxColumn.ElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding Path=DataContext.MaterialItems,ElementName=grdBeam}" /> </Style> </DataGridComboBoxColumn.ElementStyle> </DataGridComboBoxColumn> </DataGrid.Columns> </DataGrid>
后端:
public partial class BeamView : Window { public BeamView() { InitializeComponent(); this.DataContext = this; } public ObservableCollection<BeamGroup> BeamCollection { get; set; } = new ObservableCollection<BeamGroup>(); /// <summary> /// 材料列表 /// </summary> public List<Element> MaterialItems { get; set; } = new List<Element>(); } public class BeamGroup { public Element BeamMaterial { get { return _beamMaterial; } set { _beamMaterial = value; } }
//.....此处省略N多个属性
private Element _beamMaterial; }
public class Element
{
public string Name { get; set; }
}