我有一个包含三列的网格:
- 友好名称
- 大陆名称
- 国家名称
第一列是可编辑的文本框列。第二列是下拉列表框,显示大陆列表。第三列是下拉列表框,根据在第二列中选择的大陆显示国家列表。我想为这些列实现单击功能。我尝试了此链接中提供的解决方案:Single click edit in WPF DataGrid,但这仅适用于第一列,而不适用于另外两个(DataGridTemplateColumn)列。
这怎么可能呢?请建议。以下是示例XAML和数据描述。
<DataGrid Grid.Row="1" VerticalAlignment="Top"
ItemsSource="{Binding Path=GeographyData,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource DataGridStyleNormal}">
<DataGrid.Columns>
<DataGridTextColumn Width="*" Header="Friendly name" Binding="{Binding Path=FriendlyName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
<!--FilterDef -->
<DataGridTemplateColumn Width="*"
Header="Continents">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox SelectedValue="{Binding ContinentId, Mode=TwoWay}"
SelectedValuePath="ID"
DisplayMemberPath="Name"
ItemsSource="{Binding Path=DataContext.ContinentsAndCountries,Mode=OneWay,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGrid}}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource ContinentCaptionConverter}">
<Binding Path="ContinentId"/>
<Binding Path="DataContext.ContinentsAndCountries" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGrid}}"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--Level-->
<DataGridTemplateColumn Width="*"
Header="Countries">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox SelectedValue="{Binding CountryId, Mode=TwoWay}"
SelectedValuePath="ID"
DisplayMemberPath="Name">
<ComboBox.ItemsSource>
<MultiBinding Converter="{StaticResource CountryValuesConverter}">
<Binding Path="DataContext.ContinentsAndCountries" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGrid}}"/>
<Binding Path="ContinentId"/>
</MultiBinding>
</ComboBox.ItemsSource>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource CountryCaptionConverter}">
<Binding Path="DataContext.ContinentsAndCountries" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGrid}}"/>
<Binding Path="ContinentId"/>
<Binding Path="CountryId"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
注意:数据“ContinentsAndCountries”是一个包含主细节数据的可观察集合。
- Girija