开发者

Bind linq to DataGridComboBoxColumn

I am using wpftoolkit from wpf.codeplex.com . While binding a data table(through linq object list) there is a collumn that contains a reference of another table content as a foreign key. So, generally, currently, on that column in datagrid, i want to show a combo box containing the element of another table and selelted item should be from my concerned table. Here is what I am writing:

<dg:DataGrid x:Name="UsersGrid" AutoGenerateColumns="False" CellEditEnding="UsersGrid_CellEditEnding" RowEditEnding="UsersGrid_RowEditEnding" PreviewKeyDow开发者_如何学编程n="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>

For making the binding, the code behind file contains as the following code:

UsersGrid.ItemsSource = UserManager.GetAllUsers();

Problem is that, I am now don't know what to place in the "ItemsSource" property to achieve what i want for the DataGridComboBoxColumn column . Can anyone please help me? Again mentioning, i want to bind it with linq object.


There is a strange bug with a DataGridComboBoxColumn: the ItemsSource property doesn't support advanced bindings. But you can use DataGridTemplateColumn, and code will be like this:

<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>

And add this code to the constructor of the View:

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

Where the MainViewModel is an auxiliary class:

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

Another way is using a list of UserViewModel instead of a list of User, where each viewmodel class contains the list of roles, so it will be possible to use a correct binding. But it is a more complicated solution than above-mentioned one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜