Reusing a Custom Style in Silverlight
Modifying RowStyle
DataGrid
, I created a custom grid that will show some buttons at the end of the line when the mouse hovers over the line:
I created a new style DataGridRow
based on the default style. Then I changed the XAML to add my buttons inside StackPanel
(details omitted):
<UserControl.Resources>
<Style x:Key="DataGridRowStyle" TargetType="swcd:DataGridRow">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="swcd:DataGridRow">
...
<StackPanel x:Name="RowControlsPanel">
<Button>
... these are the buttons displayed on the row
DataGrid
changes using style:
<swcd:DataGrid RowStyle="{StaticResource DataGridRowStyle}">
...
</swcd:DataGrid>
I want to create another grid in a similar way, but with a different set of buttons at the end of the line. I could create a text copy of my style and modify it accordingly, but I was hoping I could create a suitable reusable class. I'm not sure how to approach this as the material I want to express from my style is a collection of controls (buttons) inside the style.
So far, my approach is to create a class MyDataGrid
derived from DataGrid
. I added a new property RowControls
to MyDataGrid
, allowing me to create it like this:
<local:MyDataGrid>
<local:MyDataGrid.RowControls>
<Button>
... these controls should go at the end of the row
</local:MyDataGrid.RowControls>
...
</local:MyDataGrid>
MyDataGrid
uses RowStyle
as described above. But how MyDataGrid.RowControls
does the contents of the collection get into Content
from RowControlsPanel
in style? I think I should do this in OnApplyTemplate
for DataGridRow
, but then I need to get the new class MyDataGridRow
from DataGridRow
. Unfortunately, it seems like it's DataGrid
hardcoded to use DataGridRow
and I can't inject my own derived string class. I feel like I need to approach my reuse problem in a different way, but I'm not sure how?
Customizing simple controls like buttons by adding new properties and changing the control template is pretty straightforward, but how do I customize a complex control like DataGrid
where the template I need to customize is nested within the grid?
source to share