在 Silverlight 中重用自定义样式

发布于 2024-08-02 11:30:22 字数 1947 浏览 1 评论 0原文

通过修改 DataGridRowStyle,我创建了一个自定义网格,当鼠标悬停在行上方时,该网格将在行末尾显示一些按钮:

Custom DataGrid

我为 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>

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

到目前为止,我的方法是创建一个从 DataGrid 派生的 MyDataGrid 类。我向 MyDataGrid 添加了一个新属性 RowControls,使我能够像这样实例化它:

<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 集合的内容如何进入样式中 RowControlsPanelContent 呢?我认为我应该在 DataGridRowOnApplyTemplate 中执行此操作,但随后我需要从 DataGridRow 派生一个新的 MyDataGridRow 类代码>.不幸的是,似乎 DataGrid 被硬编码为使用 DataGridRow 并且我无法注入我自己的派生行类。我觉得我需要以不同的方式解决重用问题,但我不确定如何?

通过添加新属性和修改控件模板来自定义按钮等简单控件非常容易,但是如何自定义像 DataGrid 这样的复杂控件,其中我需要自定义的模板嵌套在网格内?

By modifying the RowStyle of a DataGrid I have created a customized grid that will display some buttons at the end of the row when the mouse hovers above the row:

Custom DataGrid

I created a new style for DataGridRow based on the default style. I then modified the XAML to add my buttons inside a 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

The DataGrid is modified using the style:

<swcd:DataGrid RowStyle="{StaticResource DataGridRowStyle}">
  ...
</swcd:DataGrid>

I want to create another grid in a similar manner, but with a different set of buttons at the end of the row. I could create a textual copy of my style and modify it accordingly, but I was hoping that I could create a proper reusable class. I'm not sure how to approach this since the stuff I want to factor out of my style is a collection of controls (buttons) inside a style.

My approach so far is to create a MyDataGrid class derived from DataGrid. I have added a new property RowControls to MyDataGrid enabling me to instantiate 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 a RowStyle as described above. But how do the contents of the MyDataGrid.RowControls collection get into the Content of RowControlsPanel in the style? I think I should do that in OnApplyTemplate of the DataGridRow, but then I need to derive a new MyDataGridRow class from DataGridRow. Unfortunately it seems that DataGrid is hardcoded to use DataGridRow and I'm not able to inject my own derived row class. I get the feeling that I need to approach my problem of reuse in a different manner, but I'm not sure how?

Customizing simple controls like buttons by adding new properties and modifying the control template is quite easy, but how do I customize a complex control like DataGrid where the template I need to customize is nested inside the grid?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初懵 2024-08-09 11:30:22

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

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

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

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

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

That technique will allow you to make minor modifications, like changing the row buttons in your example, to an existing style.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文