0
我的要求是根據條件對數據網格行進行分組,並以交替的顏色顯示分組的行以及每個組的編輯按鈕。這裏的問題是我的datagrid列標題不與數據網格數據行對齊。 Current Grid使用分組按鈕對齊數據網格的列標題
我已經經歷了很多帖子沒有太多的幫助,我也試過這個帖子以及WPF DataGrid GroupStyle。這個例子有一個網格組風格,但我試圖做一個數據網格相同。
當前XAML
<Window x:Class="TimeSeriesDataGrid.View.TimeSeriesView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Width="Auto" Height="Auto"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:viewmodel="clr-namespace:TimeSeriesDataGrid.ViewModel"
xmlns:converter="clr-namespace:TimeSeriesDataGrid.Converters"
xmlns:local="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" Title="{Binding WindowTitle}" ResizeMode="CanMinimize">
<Window.DataContext>
<viewmodel:TimeSeriesViewModel />
</Window.DataContext>
<Window.Background>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="1"/>
<GradientStop Color="#FFEEEFFF" Offset="0.836"/>
</LinearGradientBrush>
</Window.Background>
<Window.Resources>
<converter:RowColorConverter x:Key="RowColorConverter" />
<CollectionViewSource x:Key="TimeSeriesCollectionViewSource" CollectionViewType="ListCollectionView" Source="{Binding GetTimeSeries}" >
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="DocumentId"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid Margin="0,0,0,-3">
<Grid.RowDefinitions>
<RowDefinition Height="8*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<DataGrid
DataContext="{StaticResource TimeSeriesCollectionViewSource}"
SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.SelectedTimeSeries}"
ItemsSource="{Binding}"
CanUserReorderColumns="False"
SnapsToDevicePixels="True"
Grid.Column="0"
Grid.Row="0"
x:Name="dg_TimeSeriesEdit"
AutoGenerateColumns="False"
CanUserResizeColumns="False"
ScrollViewer.IsDeferredScrollingEnabled="True"
VirtualizingStackPanel.IsVirtualizing="true"
HeadersVisibility="Column"
HorizontalContentAlignment="Center"
CanUserAddRows="False"
CanUserDeleteRows="False"
MaxHeight="800"
MaxWidth="2550"
HorizontalGridLinesBrush="#FFC92222"
VerticalGridLinesBrush="#FFC92222"
Width="Auto"
Height="Auto" >
<DataGrid.GroupStyle>
<GroupStyle AlternationCount="2" >
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<StackPanel Orientation="Vertical" VerticalAlignment="Center" Background="Transparent">
<Button Grid.Column="0" BorderThickness="0" Content="Edit" Margin="0,0,0,5" Height="Auto" Width="50" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<local:EventToCommand Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.EditGroupCmd}" CommandParameter="{Binding Path=Items}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
<ItemsPresenter />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.RowStyle>
<Style>
<Setter Property="DataGridRow.Background"
Value="{Binding RelativeSource={RelativeSource AncestorType=GroupItem},
Path=(ItemsControl.AlternationIndex), Converter={StaticResource RowColorConverter}}"/>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns >
.
.
</DataGrid.Columns>
</DataGrid>
</Grid>
</Grid>
</Window>
任何幫助,將不勝感激。
@Michal Ciechan你的回答讓我一個正常的電網解決方案,我試圖做的DataGrid相同的,但不可能使它發揮作用。 ..http://stackoverflow.com/questions/22686170/wpf-datagrid-group-style – Arun