【转】WPFDataGridComboBoxColumn使⽤
若要填充下拉列表,请⾸先使⽤下列选项之⼀设置 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>columnspan是什么意思
原⽂地址:
根据上述⽂章,我试了⼀下简化,可以实现效果
前端:(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; }
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论