开发者

How to use WPF Toolkit Datagrid DataGridTemplateColumn and a combobox?

开发者 https://www.devze.com 2022-12-11 09:11 出处:网络
I have a data grid that looks like this <tk:DataGridItemsSource=\"{Binding Parents}\" AutoGenerateColumns=\"False\">

I have a data grid that looks like this

<tk:DataGrid  ItemsSource="{Binding Parents}" AutoGenerateColumns="False">
                <tk:DataGrid.Columns>
                <tk:DataGridTextColumn  Header="Description" Binding="{Binding ID}" />

                <tk:DataGridTemplateColumn Header="Description" >
                    <tk:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=Description, Mode=TwoWay}" />
                        </DataTemplate>
                    </tk:Da开发者_如何转开发taGridTemplateColumn.CellEditingTemplate>
                    <tk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Description}"/>
                        </DataTemplate>
                    </tk:DataGridTemplateColumn.CellTemplate>
                </tk:DataGridTemplateColumn>

                <tk:DataGridTemplateColumn Header="Child Description" >
                    <tk:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox  SelectedIndex="{Binding Path=ChildID}"  ItemsSource="{Binding Path=Children}" />
                        </DataTemplate>
                    </tk:DataGridTemplateColumn.CellEditingTemplate>
                    <tk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Children.Description}"/>
                        </DataTemplate>
                    </tk:DataGridTemplateColumn.CellTemplate>
                </tk:DataGridTemplateColumn>
            </tk:DataGrid.Columns>
        </tk:DataGrid>

The view is bound to a ViewModel that exposes a list of Parents which should be my rows and a Children list which is supposed to be the combobox dropdown contents. The way it is set up I get my rows of Parents, but no data in the Child Description column. When I double click the row becomes editable and the combobox shows up. But no data. When I look in the output window I see the binding error Saying "BindingExpression path error: 'Children' property not found on 'object' ''Parent' ". I know...How do I tell it to look up one level? I've tried binding the datagrid to just the viewmodel, but then no rows show up. I've tried using the relativesource markup and I still can't get it to see what I want it to see. I'm sure my syntax is incorrect. And I could not find any examples. What am I doing wrong?


There may be smarter ways of accomplishing this task, but the stupid, quick method I would use is to modify the Parent object to contain a Children collection. This would make the relationship of Parent and Child explicit and you don't have to change your xaml syntax from above.

If you didn't want to add a Children collection to your Parent object, you may be able to use the following xaml binding::

    ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, 
AncestorType={x:Type tk:DataGrid}}, 
Path=DataContext.Children}"
0

精彩评论

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