【问题标题】:Bind linq to DataGridComboBoxColumn将 linq 绑定到 DataGridComboBoxColumn
【发布时间】:2011-07-10 21:16:59
【问题描述】:

我正在使用来自 wpf.codeplex.com 的 wpftoolkit。在绑定数据表(通过 linq 对象列表)时,有一个列包含另一个表内容的引用作为外键。所以,一般来说,目前,在数据网格中的该列上,我想显示一个组合框,其中包含另一个表的元素,并且选择的项目应该来自我关注的表。这是我要写的:

<dg:DataGrid x:Name="UsersGrid" AutoGenerateColumns="False" CellEditEnding="UsersGrid_CellEditEnding" RowEditEnding="UsersGrid_RowEditEnding" PreviewKeyDown="UsersGrid_PreviewKeyDown">
                <dg:DataGrid.Columns>
                    <dg:DataGridTextColumn Binding="{Binding Path=Id}" Header="Id" Visibility="Hidden" />
                    <dg:DataGridTextColumn Binding="{Binding Path=Username}" Header="User Name" />
                    <dg:DataGridTextColumn Binding="{Binding Path=Password}" Header="Password" />
                    <dg:DataGridTextColumn Binding="{Binding Path=RoleId}" Header="Role ID" />
                    <dg:DataGridComboBoxColumn x:Name="Roles" ItemsSource="{Binding Path=TVRole}">                            

                    </dg:DataGridTemplateColumn>
                </dg:DataGrid.Columns>
            </dg:DataGrid>

为了进行绑定,文件后面的代码包含如下代码:

UsersGrid.ItemsSource = UserManager.GetAllUsers();

问题是,我现在不知道在“ItemsSource”属性中放置什么来实现我想要的 DataGridComboBoxColumn 列。谁能帮帮我吗?再次提及,我想将它与 linq 对象绑定。

【问题讨论】:

    标签: wpf wpf-controls binding wpfdatagrid wpftoolkit


    【解决方案1】:

    DataGridComboBoxColumn 存在一个奇怪的错误:ItemsSource 属性不支持高级绑定。但是你可以使用DataGridTemplateColumn,代码会是这样的:

    <DataGrid ItemsSource="{Binding Users}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=Id}" Header="Id" Visibility="Hidden" />
            <DataGridTextColumn Binding="{Binding Path=Username}" Header="User Name" />
            <DataGridTextColumn Binding="{Binding Path=Password}" Header="Password" />
            <DataGridTextColumn Binding="{Binding Path=RoleId}" Header="Role ID" />
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Path=DataContext.Roles, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}"
                                  DisplayMemberPath="Title" SelectedValuePath="Id" SelectedValue="{Binding RoleId, Mode=TwoWay}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
    

    并将这段代码添加到视图的构造函数中:

     var model = new MainViewModel() { Users = UserManager.GetAllUsers(), Roles = UserManager.GetAllRoles() };
     this.DataContext = model;
    

    MainViewModel 是一个辅助类:

    public class MainViewModel
    {
        public List<Role> Roles { get; set; }
        public List<User> Users { get; set; }
    }
    

    另一种方法是使用UserViewModel 列表而不是User 列表,其中每个视图模型类都包含角色列表,因此可以使用正确的绑定。但它是一种比上述解决方案更复杂的解决方案。

    【讨论】:

      猜你喜欢
      • 2011-12-21
      • 2020-09-27
      • 2018-03-12
      • 2020-08-14
      • 2013-04-24
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      相关资源
      最近更新 更多