感謝您閱讀我的問題!
我有一個自定義DataGrid RowGroupHeader的問題,情況是:
我需要一種控制DataGrid RowGroupHeader的方式,因爲我想在其中顯示一些數據綁定值。這些值將在運行時生成,因此需要在運行時生成RowGroupHeaderStyle並將其添加到DataGrid的RowGroupHeaderStyles屬性中(可能使用XamlReader.Load)。
此外,RowGroupHeader內的數據綁定值的位置應與DataGrid中的相應列對齊。
我已經成功地創建一個使用這個帖子MS Forum工作模板,但我曾與定位數據綁定的元素,在我的情況的TextBlock沒有運氣,正確地與給定的DataGrid列對齊。
有什麼建議嗎?
非常感謝如何爲Silverlight 3自定義DataGrid RowHeaderGroup創建模板?
1
A
回答
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 +
那是非常多的,是的,我同意這不是一項簡單的工作,但它可以讓你在需要時自定義風格!歡迎評論!
+0
忘了添加到這個:在運行時使用XamlReader.Load加載樣式(在我的情況下)或者將它作爲ResourceDictionary或類似的XAML – KlsLondon 2010-11-12 15:49:49
相關問題
- 1. Silverlight:如何爲模板/自定義控件創建VisualState
- 2. 如何爲silverlight datagrid列指定模板?
- 3. 如何創建CakePHP的3個自定義模板
- 4. 爲SharePoint創建自定義ItemStyle模板
- 5. 爲KendoUI創建自定義模板ListView
- 6. 自定義Silverlight TreeView模板
- 7. Silverlight 3 Datagrid:模板行容器
- 8. 如何爲WCSF(.NET 2.0)創建自定義模板?
- 9. 如何爲Xcode 6創建自定義Swift文件模板?
- 10. 如何爲自定義頁面模板創建簡碼?
- 11. 如何爲內聯變量模板創建自定義點?
- 12. 如何爲自定義FormType創建表單樹枝模板?
- 13. Datagrip,創建自定義模板
- 14. DataGrid的自定義列模板
- 15. 創建自定義Xcode項目模板?
- 16. 創建自定義頁面模板
- 17. 自定義GUI模板腳本創建
- 18. XCode 4.2自定義模板創建?
- 19. Silverlight Datagrid:使用自定義行爲設置模板單元格的背景
- 20. 如何在Silverlight中創建UserControl模板
- 21. Silverlight:自定義DatePicker模板的問題
- 22. 如何在ASP.NET MVC 3中創建自定義編輯器/顯示模板?
- 23. 創建一個自定義DataGrid的ItemsSource
- 24. Silverlight爲TabControl創建控件模板
- 25. CakePHP 3:如何創建自定義模型回調?
- 26. Silverlight 3 - 綁定到DataGrid
- 27. silverlight 3 Datagrid標題定製
- 28. 在drupal中爲自定義塊創建一個自定義模板文件
- 29. 如何創建自定義模塊
- 30. 如何創建自定義模態NSWindow?
您可以展開__Row__頭中的內容如何與特定的__Column__對齊嗎?你的意思是垂直的嗎?這是非常高的順序。還有一些原因標題綁定的行標題中的內容將無法正常工作?或者你是否說要顯示的潛在綁定集可能會每行有所不同? – AnthonyWJones 2010-10-01 10:04:11