Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF样式基于当前上下文中的父样式_Wpf_Styles_Basedon - Fatal编程技术网

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>


我想你理解错了我的问题,请参考我的说明,为什么不给它一把钥匙,并在需要的地方使用它呢?