【问题标题】:How to apply WPF styles based on top of another style without using BasedOn property如何在不使用 BasedOn 属性的情况下基于另一种样式应用 WPF 样式
【发布时间】:2013-04-22 19:42:42
【问题描述】:

我正在设计 CellValuePresenter(来自 Infragistics)以赋予 Gid Lines 不同的外观,并定义了一个样式 (gridLineStyle) 并应用于 Grid 的 CellValuePresenterStyle 属性。

我发现有些列的自定义模板是通过模板化 CellValuePrenter 定义的,并且网格线不可见(如预期的那样)。我可以通过应用 BasedOn 属性使其工作,如

 <Style x:Key="gridLineStyle" TargetType="ig:CellValuePresenter">
      <Setter Property="BorderThickness" Value="0,0,1,1"/>
      <Setter Property="BorderBrush" Value="{Binding Path=BorderBrushForAllCells,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type pwc:CarbonBlotter}}}"/> 
    </Style>

 <Style x:Key="anotherColumnStyle" TargetType="{x:Type ig:CellValuePresenter}"   BasedOn="{StaticResource gridLineStyle}">
      <Setter Property="Template">
      ....

<pwc:BaseXamDataGrid>
    <pwc:BaseXamDataGrid.FieldSettings>
            <ig:FieldSettings CellValuePresenterStyle="{StaticResource gridLineStyle}"
             ...

但是自定义模板有很多样式,我只是想知道我是否可以在不使用 BasedOn 属性但继承默认样式的情况下定义样式

【问题讨论】:

  • 如果不重新创建整个样式定义,您可能无法跳过 BasedOn,这可能需要做很多工作。您可以做的是您可以跳过 x:Key 属性,并且此样式将成为其范围内所有 ig:CellValuePresenters 的默认样式
  • @StenPetrov 如果我从 gridLinesStyles 中删除 x:key,它将成为所有 CellValuePresenters 的默认值,除了模板化的那些(如在 anotherColumnStyle 中),它不会继承默认样式,不是吗。这正是我想要解决的问题。
  • 正确。如果您在某处使用了 Style 属性,则默认样式不会是这个。
  • Infragistics 不提供带有样式的 xaml 资源文件吗?如果是这样,您可以修改 xaml 文件以满足您的需求...
  • @DeanKuga 当您像在 anotherColumnStyle 中一样重新设置它的样式时,它将不再尊重原始样式,不是吗?

标签: wpf


【解决方案1】:

您可以在 DefaultStyles\DataPresenter\DataPresenterGeneric_Express.xaml 下的 infragistics 安装文件夹中找到完整的 CellValuePresenter 样式定义 您可以将该样式复制到 Application.Resources 下的 App.xaml 中,根据需要进行修改,这应该成为 CellValuePresenter 的新默认样式。

【讨论】:

  • 当您像在 anotherColumnStyle 中一样重新设置它的样式时,它将不再尊重原始样式,不是吗?
  • 如果您将样式定义放在Application.Resources中,该样式将成为新的“原始样式”。这不是 Infragistics 在安装过程中提供 DefaultStyles 文件夹的原因。因此,您可以根据需要重新设计所有内容。如果您不需要在全局应用程序级别更改样式,则可以放入 UserControl 的 Resources 甚至 XamDataGrid 的资源中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-30
  • 2011-01-27
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
  • 2018-04-26
相关资源
最近更新 更多