【发布时间】:2014-04-24 09:46:17
【问题描述】:
是否可以在数据网格组标题中使用组合框为相应列的每个单元格设置一个选定的值(这个包含组合框)? 或者有没有更好的解决方案同时在一个列中设置多个组合框值?
【问题讨论】:
-
几乎一切皆有可能。但是,如果您想认真回答,则需要put some more effort into your question。
是否可以在数据网格组标题中使用组合框为相应列的每个单元格设置一个选定的值(这个包含组合框)? 或者有没有更好的解决方案同时在一个列中设置多个组合框值?
【问题讨论】:
您可以像这样在组标题中添加ComboBox:
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander>
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" VerticalAlignment="Center" />
<TextBlock Text="{Binding Path=ItemCount}" Margin="5,0,2,0" VerticalAlignment="Center" />
<TextBlock Text="Items" VerticalAlignment="Center" />
<ComboBox Margin="10,0" SelectionChanged="ComboBox_SelectionChanged">
<ComboBoxItem Content="1" />
<ComboBoxItem Content="2" />
<ComboBoxItem Content="3" />
</ComboBox>
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
SelectionChanged 事件处理程序如下所示:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DependencyObject dependencyObject = sender as DependencyObject;
// Walk up the visual tree from the ComboBox to find the GroupItem.
while (dependencyObject != null && dependencyObject.GetType() != typeof(GroupItem))
{
dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
}
GroupItem groupItem = dependencyObject as GroupItem;
CollectionViewGroup collectionViewGroup = groupItem.Content as CollectionViewGroup;
// Iterate the items of the CollectionViewGroup for the GroupItem.
foreach (object item in collectionViewGroup.Items)
{
// Your logic for changing values here...
}
}
【讨论】: