如何为Silverlight 3自定义DataGrid RowHeaderGroup创建模板?

时间:2010-10-01 09:35:16

标签: wpf silverlight datagrid

感谢您阅读我的问题!
我有自定义DataGrid RowGroupHeader的问题,情况是:
我需要一种控制DataGrid RowGroupHeader的方法,因为我想在其中显示一些数据绑定值。这些值将在运行时生成,因此需要在运行时生成RowGroupHeaderStyle并将其添加到DataGrid的RowGroupHeaderStyles属性中(可能使用XamlReader.Load)。 此外,RowGroupHeader中数据绑定值的位置应与DataGrid中的相应列对齐。
我已经设法使用这篇文章MS Forum创建了一个工作模板,但我没有好运定位数据绑定元素,在我的案例中是TextBlock,以正确对齐给定的DataGrid列。
有什么建议吗? 非常感谢

1 个答案:

答案 0 :(得分:0)

这不是一件容易的事,但经过一些广泛的谷歌搜索和拼凑其他人的经历后,我想出了这个对我来说很好的工作

string loadString = @"<Style xmlns=""http://schemas.microsoft.com/client/2007"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" 
                                                 xmlns:localprimitives=""clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data""
xmlns:vsm=""clr-namespace:System.Windows;assembly=System.Windows""
xmlns:data=""clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"" TargetType=""data:DataGridRowGroupHeader"">
                <Setter Property=""Cursor"" Value=""Arrow"" />
                <Setter Property=""IsTabStop"" Value=""False"" />
                <Setter Property=""Background"" Value=""#FFE4E8EA"" />
                <Setter Property=""Height"" Value=""20""/>              
                <Setter Property=""Template"">
                    <Setter.Value>
                        <ControlTemplate TargetType=""data:DataGridRowGroupHeader"">
                            <localprimitives:DataGridFrozenGrid Name=""Root"" Background=""{TemplateBinding Background}"">
                                <vsm:VisualStateManager.VisualStateGroups>
                                    <vsm:VisualStateGroup x:Name=""CurrentStates"">
                                        <vsm:VisualState x:Name=""Regular""/>
                                        <vsm:VisualState x:Name=""Current"">
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName=""FocusVisual"" Storyboard.TargetProperty=""Opacity"" To=""1"" Duration=""0"" />
                                            </Storyboard>
                                        </vsm:VisualState>
                                    </vsm:VisualStateGroup>
                                </vsm:VisualStateManager.VisualStateGroups>
                                <localprimitives:DataGridFrozenGrid.Resources>

                                </localprimitives:DataGridFrozenGrid.Resources>

                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width=""Auto"" />
                                    <ColumnDefinition Width=""Auto"" />
                                    <ColumnDefinition Width=""Auto"" />
                                    <ColumnDefinition Width=""Auto"" />
                                    <ColumnDefinition Width=""Auto"" />
                                    <ColumnDefinition Width=""Auto"" />"     

此时我在网格中有一些动态列,所以我将它们添加为Grid.ColumnDefinitions到目前为止,但如果你的数据网格是静态的,那么你输入正确数量的列。

+ colStr1 +
                               @"</Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height=""Auto""/>
                                    <RowDefinition/>
                                    <RowDefinition Height=""Auto""/>
                                </Grid.RowDefinitions>
                                <StackPanel x:Name=""CustomRGHStackPanel"" Orientation=""Horizontal"" Tag=" + _tagNo + @"  Grid.Column=""3"" Grid.Row=""1""  VerticalAlignment=""Center"" Margin=""0,1,0,1"">
                                    <StackPanel.Resources>
                                        <Style  TargetType=""data:DataGridCell"">
                                            <Setter Property=""Background"" Value=""Transparent"" />
                                            <Setter Property=""HorizontalContentAlignment"" Value=""Stretch"" />
                                            <Setter Property=""VerticalContentAlignment"" Value=""Stretch"" />
                                            <Setter Property=""IsTabStop"" Value=""False"" />
                                            <Setter Property=""FontWeight"" Value=""Black""/>
                                            <Setter Property=""Template"">
                                                <Setter.Value>
                                                    <ControlTemplate TargetType=""data:DataGridCell"">
                                                        <Grid Margin=""1,-1,-1,0"" >
                                                            <Grid Name=""Root"" Background=""{TemplateBinding Background}"" Margin=""5,0,0,0"">
                                                                <vsm:VisualStateManager.VisualStateGroups>
                                                                    <vsm:VisualStateGroup x:Name=""CurrentStates"">
                                                                        <vsm:VisualState x:Name=""Regular"" />
                                                                        <vsm:VisualState x:Name=""Current"">
                                                                            <Storyboard>
                                                                                <DoubleAnimation Storyboard.TargetName=""FocusVisual"" Storyboard.TargetProperty=""Opacity"" To=""1"" Duration=""0"" />
                                                                            </Storyboard>
                                                                        </vsm:VisualState>
                                                                    </vsm:VisualStateGroup>
                                                                    <vsm:VisualStateGroup x:Name=""ValidationStates"">
                                                                        <vsm:VisualState x:Name=""Valid""/>
                                                                        <vsm:VisualState x:Name=""Invalid"">
                                                                            <Storyboard>
                                                                                <DoubleAnimation Storyboard.TargetName=""InvalidVisualElement"" Storyboard.TargetProperty=""Opacity"" Duration=""0"" To=""1""/>
                                                                                <ColorAnimation Storyboard.TargetName=""FocusVisual"" Storyboard.TargetProperty=""(Fill).Color"" Duration=""0"" To=""#FFFFFFFF""/>
                                                                            </Storyboard>
                                                                        </vsm:VisualState>
                                                                    </vsm:VisualStateGroup>
                                                                </vsm:VisualStateManager.VisualStateGroups>

                                                                <Grid.ColumnDefinitions>
                                                                    <ColumnDefinition />
                                                                    <ColumnDefinition Width=""Auto"" />
                                                                </Grid.ColumnDefinitions>

                                                                <Rectangle Name=""FocusVisual"" Stroke=""#FF6DBDD1"" StrokeThickness=""1"" Fill=""#66FFFFFF"" HorizontalAlignment=""Stretch"" 
                           VerticalAlignment=""Stretch"" IsHitTestVisible=""false"" Opacity=""0"" />

                                                                <ContentPresenter
                    Content=""{TemplateBinding Content}""
                    ContentTemplate=""{TemplateBinding ContentTemplate}""
                    HorizontalAlignment=""{TemplateBinding HorizontalContentAlignment}""
                    VerticalAlignment=""{TemplateBinding VerticalContentAlignment}""
                    Margin=""{TemplateBinding Padding}"" />

                                                                <Rectangle x:Name=""InvalidVisualElement"" IsHitTestVisible=""False"" StrokeThickness=""1"" Stroke=""#FFDC000C"" HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Opacity=""0""/>
                                                                <Rectangle Name=""q1q"" Fill=""#c9caca"" Grid.Column=""1"" Margin=""1,0,1,0"" HorizontalAlignment=""Right"" VerticalAlignment=""Stretch"" Width=""0"" />

                                                            </Grid>
                                                            <Rectangle Name=""qq"" Fill=""#c9caca""  Margin=""1,0,1,0"" HorizontalAlignment=""Left"" VerticalAlignment=""Stretch"" Width=""1"" />

                                                        </Grid>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </StackPanel.Resources>
                                    <data:DataGridCell  Content=""{Binding Name}""/>
                                    <data:DataGridCell  Content=""""/>
                                    <data:DataGridCell  Content=""""/>
                                    <data:DataGridCell  Content=""""/>
                                    <data:DataGridCell  Content=""""/>
                                    <data:DataGridCell HorizontalContentAlignment=""Left"" Margin=""7,0,0,0"" Content=""{Binding Converter={StaticResource myConverter}, ConverterParameter=TotalScore}""/>" + colStr2 +
                                @"</StackPanel>
                                <Rectangle Grid.Column=""1"" Grid.ColumnSpan=""5"" Fill=""#FFFFFFFF"" Height=""1""/>
                                <Rectangle Grid.Column=""1"" Grid.Row=""1"" Name=""IndentSpacer"" />
                                <ToggleButton Grid.Column=""2"" Grid.Row=""1"" Name=""ExpanderButton"" Height=""15"" Width=""15"" Margin=""2,0,0,0"">
                                  <ToggleButton.Template>
                                    <ControlTemplate TargetType=""ToggleButton"">
                                      <Grid Background=""Transparent"">
            <vsm:VisualStateManager.VisualStateGroups>
                <vsm:VisualStateGroup x:Name=""CommonStates"">
                    <vsm:VisualState x:Name=""Normal""/>
                    <vsm:VisualState x:Name=""MouseOver"">
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetName=""CollapsedArrow"" Storyboard.TargetProperty=""(Stroke).Color"" Duration=""0"" To=""#FF6DBDD1""/>
                            <ColorAnimation Storyboard.TargetName=""ExpandedArrow"" Storyboard.TargetProperty=""(Fill).Color"" Duration=""0"" To=""#FF6DBDD1""/>
                        </Storyboard>
                    </vsm:VisualState>
                    <vsm:VisualState x:Name=""Pressed"">
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetName=""CollapsedArrow"" Storyboard.TargetProperty=""(Stroke).Color"" Duration=""0"" To=""#FF6DBDD1""/>
                            <ColorAnimation Storyboard.TargetName=""ExpandedArrow"" Storyboard.TargetProperty=""(Fill).Color"" Duration=""0"" To=""#FF6DBDD1""/>
                        </Storyboard>
                    </vsm:VisualState>
                    <vsm:VisualState x:Name=""Disabled"">
                        <Storyboard>
                            <DoubleAnimation Duration=""0"" Storyboard.TargetName=""CollapsedArrow"" Storyboard.TargetProperty=""Opacity"" To="".5""/>
                            <DoubleAnimation Duration=""0"" Storyboard.TargetName=""ExpandedArrow"" Storyboard.TargetProperty=""Opacity"" To="".5""/>
                        </Storyboard>
                    </vsm:VisualState>
                </vsm:VisualStateGroup>
                <vsm:VisualStateGroup x:Name=""CheckStates"">
                    <vsm:VisualState x:Name=""Checked"" />
                    <vsm:VisualState x:Name=""Unchecked"">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Duration=""0"" Storyboard.TargetName=""CollapsedArrow"" Storyboard.TargetProperty=""Visibility"">
                                <DiscreteObjectKeyFrame KeyTime=""0"" Value=""Visible""/>
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Duration=""0"" Storyboard.TargetName=""ExpandedArrow"" Storyboard.TargetProperty=""Visibility"">
                                <DiscreteObjectKeyFrame KeyTime=""0"" Value=""Collapsed""/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </vsm:VisualState>
                </vsm:VisualStateGroup>
            </vsm:VisualStateManager.VisualStateGroups>

            <Path Stretch=""Uniform"" Data=""F1 M 0,0 L 0,1 L .6,.5 L 0,0 Z"" Width=""5"" HorizontalAlignment=""Center"" VerticalAlignment=""Center"" x:Name=""CollapsedArrow"" Visibility=""Collapsed"" Stroke=""#FF414345""/>
            <Path Stretch=""Uniform"" Data=""F1 M 0,1 L 1,1 L 1,0 L 0,1 Z"" Width=""6"" HorizontalAlignment=""Center"" VerticalAlignment=""Center"" x:Name=""ExpandedArrow"" Fill=""#FF414345""/>
        </Grid>
          </ControlTemplate>
        </ToggleButton.Template>
        </ToggleButton>
                                <Rectangle Grid.Column=""1"" Grid.ColumnSpan=""5"" Fill=""#FFD3D3D3"" Height=""1"" Grid.Row=""2""/>
                                <Rectangle Name=""FocusVisual"" Grid.Column=""1"" Grid.ColumnSpan=""4"" Grid.RowSpan=""3"" Stroke=""#FF6DBDD1"" StrokeThickness=""1"" HorizontalAlignment=""Stretch"" 
                           VerticalAlignment=""Stretch"" IsHitTestVisible=""false"" Opacity=""0"" />
                                <localprimitives:DataGridRowHeader Name=""RowHeader""  Grid.RowSpan=""3"" localprimitives:DataGridFrozenGrid.IsFrozen=""True""/>

                            </localprimitives:DataGridFrozenGrid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>";   

这是你的单元格内容的来源,我又有一些动态列,所以我需要动态添加单元格(如果你的网格是静态的,不需要这样做)

<data:DataGridCell  Content=""{Binding Name}""/>
                                <data:DataGridCell  Content=""""/>
                                <data:DataGridCell  Content=""""/>
                                <data:DataGridCell  Content=""""/>
                                <data:DataGridCell  Content=""""/>
                                <data:DataGridCell HorizontalContentAlignment=""Left"" Margin=""7,0,0,0"" Content=""{Binding Converter={StaticResource myConverter}, ConverterParameter=TotalScore}""/>" + colStr2 +    

就是这样,是的,我同意这不是一项微不足道的工作,但它可以在您需要时为您提供定制风格!随意评论!