0

このようなデータグリッドがあります

<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:DataGridTemplateColumn.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>

ビューは、私の行であるはずの親のリストと、コンボボックスのドロップダウンコンテンツであるはずの子リストを公開するViewModelにバインドされています。設定方法では、親の行が取得されますが、[子の説明]列にデータがありません。ダブルクリックすると、行が編集可能になり、コンボボックスが表示されます。しかし、データはありません。出力ウィンドウを見ると、「BindingExpression path error:'Children' property not foundon'object''Parent'」というバインディングエラーが表示されます。私は知っています...1つのレベルを検索するように指示するにはどうすればよいですか?データグリッドをビューモデルだけにバインドしようとしましたが、行が表示されません。私はrelativesourceマークアップを使用しようとしましたが、それでも私が見たいものを見ることができません。私の構文は間違っていると確信しています。そして、私は例を見つけることができませんでした。

4

1 に答える 1

1

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}"
于 2009-11-23T18:51:45.503 に答える