当前上下文中的 WPF Style BasedOn parent Style
WPF Style BasedOn parent Style in current context
说,我有一个 TextBox
'TextBoxStyleBase' 的默认样式。
然后我定义了一个 DataGrid
样式,它有一个自己的 TextBox
样式基于那个 Base-style,定义另一个边框颜色。
在 DataGrid
中的某个地方,我想定义另一种 TextBox
样式,但继承自 DataGrid
样式中定义的样式。
有没有办法使样式继承当前为特定控件在当前 'context' 中定义的样式?
编辑:
为了更清楚,这是我的:
<!-- explicit style for all TextBoxes -->
<Style TargetType="{x:Type TextBox}" x:Key="TextStyle">
<Setter Property="FontSize" Value="16"/>
</Style>
<!-- implicit style for all TextBoxes -->
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextStyle}"/>
<!-- DataGrid style changing inner TextBox style -->
<Style TargetType="{x:Type DataGrid}">
<Style.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextStyle}">
<Setter Property="FontSize" Value="20"/>
</Style>
<!-- since TextBox has defined implicit style this would be equivalent to -->
<!--<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="FontSize" Value="20"/>
</Style>-->
</Style.Resources>
</Style>
<Control>
<DataGrid>
<Row>
<TextBox/> <!-- should show as defined in DataGrid style -->
</Row>
<Row>
<Row.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn=" ??? ">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</Style.Triggers>
</Style>
</Row.Resources>
<TextBox/> <!-- should show with additional trigger -->
</Row>
</DataGrid>
</Control>
要在 BasedOn = '???' 中输入什么以便文本以 FontSize 20 显示,但如果悬停则显示为粗体。
请为数据网格内的文本框使用以下内容:
<Style TargetType="TextBox" BasedOn="{StaticResource <your style name>}">
PS:在您的情况下将是 TextBoxStyleBase。
您不能在同一个 ResourceDictionary
中添加两个具有相同键的 Styles
。因此,如果您已经在 ResourceDictionary
中为特定类型定义了一个没有 x:Key
的隐式 Style
,则不能将另一个添加到相同的 ResourceDictionary
.
否则,您应该能够 Style
基于范围内的默认样式,如下所示:
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Triggers>
</Style.Triggers>
</Style>
说,我有一个 TextBox
'TextBoxStyleBase' 的默认样式。
然后我定义了一个 DataGrid
样式,它有一个自己的 TextBox
样式基于那个 Base-style,定义另一个边框颜色。
在 DataGrid
中的某个地方,我想定义另一种 TextBox
样式,但继承自 DataGrid
样式中定义的样式。
有没有办法使样式继承当前为特定控件在当前 'context' 中定义的样式?
编辑:
为了更清楚,这是我的:
<!-- explicit style for all TextBoxes -->
<Style TargetType="{x:Type TextBox}" x:Key="TextStyle">
<Setter Property="FontSize" Value="16"/>
</Style>
<!-- implicit style for all TextBoxes -->
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextStyle}"/>
<!-- DataGrid style changing inner TextBox style -->
<Style TargetType="{x:Type DataGrid}">
<Style.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextStyle}">
<Setter Property="FontSize" Value="20"/>
</Style>
<!-- since TextBox has defined implicit style this would be equivalent to -->
<!--<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="FontSize" Value="20"/>
</Style>-->
</Style.Resources>
</Style>
<Control>
<DataGrid>
<Row>
<TextBox/> <!-- should show as defined in DataGrid style -->
</Row>
<Row>
<Row.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn=" ??? ">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</Style.Triggers>
</Style>
</Row.Resources>
<TextBox/> <!-- should show with additional trigger -->
</Row>
</DataGrid>
</Control>
要在 BasedOn = '???' 中输入什么以便文本以 FontSize 20 显示,但如果悬停则显示为粗体。
请为数据网格内的文本框使用以下内容:
<Style TargetType="TextBox" BasedOn="{StaticResource <your style name>}">
PS:在您的情况下将是 TextBoxStyleBase。
您不能在同一个 ResourceDictionary
中添加两个具有相同键的 Styles
。因此,如果您已经在 ResourceDictionary
中为特定类型定义了一个没有 x:Key
的隐式 Style
,则不能将另一个添加到相同的 ResourceDictionary
.
否则,您应该能够 Style
基于范围内的默认样式,如下所示:
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Triggers>
</Style.Triggers>
</Style>