WPF中的DataGridComboBoxColumn SelectedValuePath [英] DataGridComboBoxColumn SelectedValuePath in WPF

查看:127
本文介绍了WPF中的DataGridComboBoxColumn SelectedValuePath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在数据网格中添加一个组合框,使用访问权限从数据库中填充它。我将该列设置为值路径并在WPF中显示成员路径?

I want to add a combo box in data grid, fill it from database using access. I set this column selected value path and display member path in WPF?

推荐答案

您必须从列的元素样式访问组合框以及用于设置items source属性的元素编辑样式。
示例:

You have to access the combobox from the column's element style and the element editing style for setting the items source property. Example:

<DataGrid Name="dgMainGrid" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Id" Binding="{Binding Id, UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn Header="Description" Binding="{Binding Description, UpdateSourceTrigger=PropertyChanged}" />
            <DataGridComboBoxColumn Header="Student List" DisplayMemberPath="Name" SelectedValuePath="Id">
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="{x:Type ComboBox}">
                        <Setter Property="ItemsSource" Value="{Binding StudentsList}" />
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="{x:Type ComboBox}">
                        <Setter Property="ItemsSource" Value="{Binding StudentsList}" />
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>
        </DataGrid.Columns>
    </DataGrid>

类之间的关系:

class SampleEntity : INotifyPropertyChanged
{
    DateTime date = DateTime.Now, then = DateTime.Now;

    #region Constructor

    public SampleEntity()
    {            
    }

    #endregion

    #region Properties

    public int Id { get; set; }

    public string Description { get; set; }

    public List<StudentEntity> StudentsList { get; set; }

    public StudentEntity SelectedStudent { get; set; }

    #endregion

    #region Notify Property Changed

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
    }

    #endregion
}

class StudentEntity : INotifyPropertyChanged
{
    DateTime date = DateTime.Now, then = DateTime.Now;

    #region Constructor

    public StudentEntity()
    {
    }

    #endregion

    #region Properties

    public int Id { get; set; }

    public string Name { get; set; }

    #endregion

    #region Notify Property Changed

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
    }

    #endregion
}

这篇关于WPF中的DataGridComboBoxColumn SelectedValuePath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆