WPF如何将DataGrid.Group标题文本实际折叠时设置 [英] WPF How to set DataGrid.Group header text when it's actually collapsed
问题描述
< DataGrid.GroupStyle>
分组我的项目,我想知道当组被折叠时如何设置标题文本。我正在将订单加载到该分组的网格中,当我折叠特定组时,我想看到类似于Order:订单数量,因为更容易定向。现在我只看到订单的数量,但是如何添加文本: < DockPanel>
< TextBlock FontWeight =BoldText ={Binding Path = Name}/> // + some text
< / DockPanel>
(另外,在添加< DataGrid.Columns>
因为我想添加我的列手动突然垂直滑块出现在屏幕上,即使没有更多的项目左或右,它看起来不好和烦人,我如何删除这个滑块,我想保留我的datagrid列,因为我想按照我想要的样式,但是我不想看到这个滑块:/?)
这是我的代码:
< Grid.ColumnDefinitions>
< ColumnDefinition Width =10 */>
< /Grid.ColumnDefinitions>
< DataGrid Grid.Column =0RowHeaderWidth =0CanUserAddRows =FalseAutoGenerateColumns =Falsex:Name =datagrid1Margin =10,150,8,50 =TransparentVerticalContentAlignment =CenterHorizontalContentAlignment =CenterItemsSource ={Binding}>
< DataGrid.Resources>
< Style TargetType ={x:Type DataGridColumnHeader}>
< Setter Property =BackgroundValue =Black/>
< Setter Property =OpacityValue =0.5/>
< Setter Property =ForegroundValue =White/>
< Setter Property =HorizontalContentAlignmentValue =Center/>
< Setter Property =FontSizeValue =15/>
< Setter Property =FontFamilyValue =Arial/>
< Setter Property =HeightValue =50/>
< / Style>
< /DataGrid.Resources>
< DataGrid.Columns>
< DataGridTextColumn Binding ={Binding ProductName}Header =TitleMinWidth =50FontSize =16FontFamily =Verdana/>
< DataGridTextColumn Binding ={Binding Quantity}Header =QuantityMinWidth =30FontSize =16FontFamily =Verdana/>
< DataGridTextColumn Binding ={Binding NumberOfOrder}Header =Order numberMinWidth =30FontSize =16FontFamily =Verdana/>
< DataGridTextColumn Binding ={Binding User}Header =UserWidth =*FontSize =16FontFamily =Verdana/>
< /DataGrid.Columns>
< DataGrid.GroupStyle>
<! - 顶级组的样式。 - >
< GroupStyle>
< GroupStyle.ContainerStyle>
< Style TargetType ={x:Type GroupItem}>
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType ={x:Type GroupItem}>
< Expander IsExpanded =True>
< Expander.Header>
& DockPanel>
< TextBlock FontWeight =BoldText ={Binding Path = Name}/> //我想知道这条线是什么意思?
< / DockPanel>
< /Expander.Header>
< Expander.Content>
< ItemsPresenter />
< /Expander.Content>
< / Expander>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>
< /GroupStyle.ContainerStyle>
< / GroupStyle>
< /DataGrid.GroupStyle>
< / DataGrid>
PS我的datagrid底部的垂直滑块的ScreenShot
我们需要一个触发器
如下所示,以及 IValueConverter
可查找组中的项目数量:
< Expander.Style>
< Style TargetType =Expander>
< Style.Triggers>
< Trigger Property =IsExpandedValue =True>
< Setter Property =Header>
< Setter.Value>
& DockPanel>
< TextBlock FontWeight =BoldText ={Binding Name}/>
< / DockPanel>
< /Setter.Value>
< / Setter>
< / Trigger>
< Trigger Property =IsExpandedValue =False>
< Setter Property =Header>
< Setter.Value>
& DockPanel>
< TextBlock FontWeight =Bold>
< TextBlock.Inlines>
< Run Text ={Binding Name}/>
< Run Text =(/>
< Run Text ={Binding Name,Converter = {StaticResource ItemCountCnvKey}}/>
< Run Text = )/>
< /TextBlock.Inlines>
< / TextBlock>
< / DockPanel>
< /Setter.Value>
< / Setter>
< / Trigger>
< /Style.Triggers>
< / Style>
< /Expander.Style>
ItemCountConverter
public class ItemCountConverter:IValueConverter
{
public object Convert(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture)
{
CollectionViewGroup group =(CollectionViewGroup)value;
ReadOnlyObservableCollection< object> items = group.Items;
return items.Count;
public object ConvertBack(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
I am working with WPF application, and I grouped my items using <DataGrid.GroupStyle>
, I am wondering how can I set header text when group is collapsed. I'm loading orders into that grouped grid, and when I collapse specific group I would like to see something like Order : number of order, because of easier orient. Right now I am seeing only number of order, but how could I add text to:
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}" /> //+ some text
</DockPanel>
(Also, after I added <DataGrid.Columns>
because I want to add my columns manualy suddenly vertical slider appear on screen even if there is no more items left or right and it looks bad and annoying, how could I remove that slider, I want to keep my datagrid columns because I want to style them as I want, but I don't want to see that slider :/ ?)
Here is my code:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*" />
</Grid.ColumnDefinitions>
<DataGrid Grid.Column="0" RowHeaderWidth="0" CanUserAddRows="False" AutoGenerateColumns="False" x:Name="datagrid1" Margin="10,150,8,50" Background="Transparent" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" ItemsSource="{Binding}">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="Black"/>
<Setter Property="Opacity" Value="0.5"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="FontSize" Value="15"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Height" Value="50"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ProductName}" Header="Title" MinWidth="50" FontSize="16" FontFamily="Verdana" />
<DataGridTextColumn Binding="{Binding Quantity}" Header="Quantity" MinWidth="30" FontSize="16" FontFamily="Verdana" />
<DataGridTextColumn Binding="{Binding NumberOfOrder}" Header="Order number" MinWidth="30" FontSize="16" FontFamily="Verdana" />
<DataGridTextColumn Binding="{Binding User}" Header="User" Width="*" FontSize="16" FontFamily="Verdana" />
</DataGrid.Columns>
<DataGrid.GroupStyle>
<!-- Style for groups at top level. -->
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True">
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}" /> //I am wondering what does this line mean?
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
P.S ScreenShot of vertical slider @ bottom of my datagrid
We need a Trigger
here as shown below, and an IValueConverter
to find number of items in a group :
<Expander.Style>
<Style TargetType="Expander">
<Style.Triggers>
<Trigger Property="IsExpanded" Value="True">
<Setter Property="Header">
<Setter.Value>
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Name}" />
</DockPanel>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsExpanded" Value="False">
<Setter Property="Header">
<Setter.Value>
<DockPanel>
<TextBlock FontWeight="Bold">
<TextBlock.Inlines>
<Run Text="{Binding Name}"/>
<Run Text=" ( "/>
<Run Text="{Binding Name, Converter={StaticResource ItemCountCnvKey}}" />
<Run Text=" ) "/>
</TextBlock.Inlines>
</TextBlock>
</DockPanel>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Expander.Style>
ItemCountConverter
public class ItemCountConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
CollectionViewGroup group = (CollectionViewGroup)value;
ReadOnlyObservableCollection<object> items = group.Items;
return items.Count;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
这篇关于WPF如何将DataGrid.Group标题文本实际折叠时设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!