所以我制作了一個 DataGrid,在其中我用一些模板對整個 DataGrid 進行了樣式化。現在我想合并每個具有相同值的單元格。我從 SQL 查詢添加資料。那是我的 DataGrid XAML 代碼:
<DataGrid.Resources>
<!--Design kopfzeile-->
<Style TargetType="{x:Type DataGridColumnHeader}" x:Name="test" >
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="LightBlue"/>
<Setter Property="FontWeight" Value="SemiBold"/>
<Setter Property="Height" Value="30"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="BorderThickness" Value="0,0,2,0" />
<Setter Property="BorderBrush" Value="#333333"/>
<Setter Property="Padding" Value="10 0 0 0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Grid x:Name="insideHeader" Background="#353E4A">
<Border x:Name="borderHeader" BorderThickness="1"
CornerRadius="6"
Background="#4F5C73"
Padding="10,0,0,0"
Margin="2">
<ContentPresenter/>
</Border>
<Thumb x:Name="PART_RightHeaderGripper" Grid.Column="1"
HorizontalAlignment="Right"
Width="2" BorderThickness="1"
BorderBrush="#353E4A"
Cursor="SizeWE"/>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=toogleButton,Path=IsChecked}" Value="False">
<Setter TargetName="borderHeader" Property="Background" Value="#FA9F34"/>
<Setter Property="Foreground" Value="#2B2B2B"/>
<Setter TargetName="insideHeader" Property="Background" Value="#00336E"/>
</DataTrigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="borderHeader" Property="Background" Value="#4182C6"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--Deaktivieren Des rowheader-->
<Style TargetType="{x:Type DataGridRowHeader}">
<Setter Property="Background" Value="Transparent"/>
</Style>
<!--Cellen Design-->
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="#3E4659"/>
<Setter Property="Foreground" Value="LightBlue"/>
<Setter Property="BorderThickness" Value="0,0,2,0" />
<Setter Property="BorderBrush" Value="#333333"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border x:Name="insideBorder" Background="#353E4A">
<Border x:Name="BorderCell" BorderThickness="1"
CornerRadius="6"
Background="#4F5C73"
Padding="10,0,0,0"
Margin="2">
<ContentPresenter/>
</Border>
</Border>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=toogleButton,Path=IsChecked}" Value="False">
<Setter TargetName="BorderCell" Property="Background" Value="#0051B0"/>
<Setter TargetName="insideBorder" Property="Background" Value="#00336E"/>
</DataTrigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="BorderCell" Property="Background" Value="#4182C6"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
我已經嘗試過類似的東西,使用 GroupStyles 但沒有用,代碼什么也沒發生:
<DataGrid ItemsSource="{Binding GroupedData}" AutoGenerateColumns="False" MinRowHeight="25" CanUserAddRows="False" CanUserDeleteRows="False">
<DataGrid.GroupStyle>
<!-- First Group -->
<GroupStyle ContainerStyle="{StaticResource GroupItemStyle}">
<GroupStyle.Panel>
<ItemsPanelTemplate>
<DataGridRowsPresenter/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
<!-- Second Group -->
<GroupStyle ContainerStyle="{StaticResource GroupItemStyle}">
<GroupStyle.Panel>
<ItemsPanelTemplate>
<DataGridRowsPresenter/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
<!-- Third Group -->
<GroupStyle ContainerStyle="{StaticResource GroupItemStyle}">
<GroupStyle.Panel>
<ItemsPanelTemplate>
<DataGridRowsPresenter/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.Columns>
...
</DataGrid.Columns>
</DataGrid>
和這里的風格:
<Style x:Key="GroupItemStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<StackPanel Orientation="Horizontal" >
<Border BorderThickness="0">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Border BorderThickness="1" MinWidth="150">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<ContentPresenter Content="{Binding Name}" >
</ContentPresenter>
</Grid>
</Border>
<Border BorderThickness="0" Grid.Column="1">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<ItemsPresenter/>
</Grid>
</Border>
</Grid>
</Border>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我試圖得到的結構看起來像帽子結構:

那么為什么這不起作用?
uj5u.com熱心網友回復:
根據您的評論,我認為您的問題具體是 DataGrid 分組對您不起作用。
如果你想使用 DataGrid 分組,你必須使用 CollectionViewSource,給它一個 GroupDescription 并將 CollectionViewSource 指向它應該使用的集合。系結時,您然后系結到 CollectionViewSource 而不是實際的集合。如果 DataGrid 沒有這樣的 GroupDescription,則 GroupStyle 將被忽略。
例如。
<Window.Resources>
<CollectionViewSource x:Key="GroupedDataViewSource" Source="{Binding GroupedData}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Country"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
并系結到它...
<DataGrid ItemsSource="{Binding Source={StaticResource ResourceKey = GroupedDataViewSource}}"/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/352564.html
