WPF样式基于当前上下文中的父样式
比如说,我有一个WPF样式基于当前上下文中的父样式,wpf,styles,basedon,Wpf,Styles,Basedon,比如说,我有一个TextBox“TextBoxStyleBase”的默认样式。 然后我定义了一个DataGrid样式,它有一个自己的TextBox样式基于该基本样式,定义了另一种边框颜色 在DataGrid中的某个地方,我想定义另一个TextBox样式,但继承自DataGrid样式中定义的样式 是否有方法使样式继承当前“上下文”中为特定控件定义的样式 编辑: 更清楚地说,我有: <!-- explicit style for all TextBoxes --> <Style T
TextBox
“TextBoxStyleBase”的默认样式。
然后我定义了一个DataGrid
样式,它有一个自己的TextBox
样式基于该基本样式,定义了另一种边框颜色
在DataGrid
中的某个地方,我想定义另一个TextBox
样式,但继承自DataGrid
样式中定义的样式
是否有方法使样式继承当前“上下文”中为特定控件定义的样式
编辑:
更清楚地说,我有:
<!-- 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
中为特定类型定义了一个隐式样式
,但没有x:Key
,则无法将另一个样式添加到同一ResourceDictionary
否则,您应该能够将样式
基于范围内的默认样式,如下所示:
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Triggers>
</Style.Triggers>
</Style>
我想你理解错了我的问题,请参考我的说明,为什么不给它一把钥匙,并在需要的地方使用它呢?