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:

Custom DataGrid

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?

+2


source to share


1 answer


Instead of creating a reusable class, you might consider reusing your style with the Silverlight 3 BasedOn style:

http://community.irritatedvowel.com/blogs/pete_browns_blog/archive/2009/03/18/Silverlight-3-1320-BasedOn-Styles.aspx



This method will allow you to make minor changes, such as changing the row buttons in your example, to an existing style.

+2


source







All Articles