通过修改RowStyle
aDataGrid
我创建了一个自定义网格,当鼠标悬停在行上方时,它将在行的末尾显示一些按钮:
DataGridRow
我基于默认样式创建了一个新样式。然后我修改了 XAML 以将我的按钮添加到一个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>
我想以类似的方式创建另一个网格,但在行的末尾有一组不同的按钮。我可以创建我的样式的文本副本并相应地修改它,但我希望我可以创建一个适当的可重用类。我不确定如何解决这个问题,因为我想从我的风格中排除的东西是风格内的控件(按钮)集合。
到目前为止,我的方法是创建一个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
如上所述使用 a 。但是集合的内容是如何MyDataGrid.RowControls
进入到Content
样式RowControlsPanel
中的呢?我想我应该在OnApplyTemplate
中这样做DataGridRow
,但是我需要MyDataGridRow
从DataGridRow
. 不幸的是,它似乎DataGrid
是硬编码的DataGridRow
,我无法注入我自己的派生行类。我觉得我需要以不同的方式解决我的重用问题,但我不确定如何?
通过添加新属性和修改控件模板来自定义按钮等简单控件非常容易,但是如何自定义复杂控件,例如DataGrid
我需要自定义的模板嵌套在网格内的位置?