gpt4 book ai didi

wpf - 在样式中设置 DataGrid.GroupStyle

转载 作者:行者123 更新时间:2023-12-02 02:22:46 27 4
gpt4 key购买 nike

我想在我的样式中设置 DataGrid.GroupStyle,这样它对于所有数据网格都是相互的,而不是在我的 View 文件中,我需要在其中分别为每个数据网格设置它。这就是我现在拥有的:

<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>

最佳答案

我需要为 ListBox 执行此操作,因此我创建了从 ListBox 继承的新控件并添加了一个可绑定(bind)和可设置样式的属性。可以对 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);
}
}
}

关于wpf - 在样式中设置 DataGrid.GroupStyle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7379630/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com