我想在我的样式中设置DataGrid.GroupStyle,因此它对于所有数据网格都是相互的,而不是在我的视图文件中,我需要将它分别设置到每个数据网格。 这就是我现在所拥有的:
<Style x:Key="DGView" TargetType="DataGrid">
<Setter Property="Height" Value="Auto" />
<Setter Property="Margin" Value="0,3,0,0" />
<Setter Property="RowHeaderWidth" Value="0" />
<Setter Property="SelectionMode" Value="Single" />
<Setter Property="SelectionUnit" Value="FullRow" />
<Setter Property="CanUserAddRows" Value="False" />
<Setter Property="CanUserDeleteRows" Value="False" />
<Setter Property="AutoGenerateColumns" Value="False" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="DataGrid.CellStyle" Value="{StaticResource DGCell}" />
</Style>
<DataGrid Style="{StaticResource DGSecondaryView}" >
<DataGrid.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource GIView}" >
<GroupStyle.Panel>
<ItemsPanelTemplate>
<DataGridRowsPresenter/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="BookID" Binding="{Binding BookID}" Visibility="Collapsed"/>
<DataGridTextColumn Header="Title" Binding="{Binding Title, Converter={StaticResource StringToOffset}}" Width="3*" IsReadOnly="True" />
<DataGridTextColumn Header="Sequence" Binding="{Binding Sequence}" Width="3*" IsReadOnly="True" />
<DataGridTextColumn Header="Number" Binding="{Binding Number}" Width="1*" IsReadOnly="True" CellStyle="{StaticResource Numeric}" />
</DataGrid.Columns>
</DataGrid>
我想要的是将以下代码插入到样式标记中。有可能吗?
<DataGrid.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource GIView}" >
<GroupStyle.Panel>
<ItemsPanelTemplate>
<DataGridRowsPresenter/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</DataGrid.GroupStyle>
答案 0 :(得分:2)
我需要为ListBox执行此操作,因此我创建了从ListBox继承并添加了可绑定和可管理属性的新控件。对于DataGrid也可以这样做。
public class MyListBox : ListBox
{
public GroupStyle DefaultGroupStyle
{
get { return (GroupStyle)GetValue(DefaultGroupStyleProperty); }
set { SetValue(DefaultGroupStyleProperty, value); }
}
// Using a DependencyProperty as the backing store for DefaultGroupStyle. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DefaultGroupStyleProperty =
DependencyProperty.Register("DefaultGroupStyle", typeof(GroupStyle), typeof(MyListBox), new UIPropertyMetadata(null, DefaultGroupStyleChanged));
private static void DefaultGroupStyleChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
((MyListBox)o).SetDefaultGroupStyle(e.NewValue as GroupStyle);
}
private void SetDefaultGroupStyle(GroupStyle defaultStyle)
{
if (defaultStyle == null)
{
return;
}
if (this.GroupStyle.Count == 0)
{
this.GroupStyle.Add(defaultStyle);
}
}
}