在Silverlight中重用自定义样式

时间:2009-08-19 13:22:16

标签: c# silverlight silverlight-3.0 datagrid controltemplate

通过修改RowStyle的{​​{1}},我创建了一个自定义网格,当鼠标悬停在该行上方时,该网格会在行尾显示一些按钮:

Custom DataGrid

我根据默认样式为DataGrid创建了一种新样式。然后我修改了XAML以在DataGridRow内添加我的按钮(详细信息省略):

StackPanel

使用样式修改<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

我想以类似的方式创建另一个网格,但在行的末尾有一组不同的按钮。我可以创建我的样式的文本副本并相应地修改它,但我希望我可以创建一个适当的可重用类。我不确定如何处理这个问题,因为我想要从我的风格中分解出来的东西是一个风格内的控件(按钮)的集合。

到目前为止,我的方法是创建一个源自<swcd:DataGrid RowStyle="{StaticResource DataGridRowStyle}"> ... </swcd:DataGrid> 的{​​{1}}类。我已向MyDataGrid添加了一个新属性DataGrid,使我能够像这样实例化它:

RowControls

MyDataGrid使用<local:MyDataGrid> <local:MyDataGrid.RowControls> <Button> ... these controls should go at the end of the row </local:MyDataGrid.RowControls> ... </local:MyDataGrid> ,如上所述。但是MyDataGrid集合的内容如何进入样式中的RowStyle MyDataGrid.RowControls?我想我应该在Content的{​​{1}}中执行此操作,但我需要从RowControlsPanel派生一个新的OnApplyTemplate类。不幸的是,似乎DataGridRow被硬编码使用MyDataGridRow并且我无法注入我自己的派生行类。我觉得我需要以不同的方式解决我的重用问题,但我不确定如何?

通过添加新属性和修改控件模板来自定义简单控件(如按钮)非常简单,但如何自定义复杂控件,如DataGridRow,我需要自定义的模板嵌套在网格中?

1 个答案:

答案 0 :(得分:2)

您可以考虑重复使用Silverlight 3 BasedOn样式的样式,而不是创建可重用的类:

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

该技术将允许您进行微小的修改,例如将示例中的行按钮更改为现有样式。

相关问题