1

比如说,我有一个TextBox“TextBoxStyleBase”的默认样式。然后我定义了一个DataGrid样式,它有一个自己的TextBox样式 BasedOn 那个 Base 样式,定义了另一个边框颜色。

在 a 内部的某个地方,DataGrid我想定义另一种TextBox样式,但继承自 style 中定义的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 显示,但如果悬停,则显示为粗体。

4

2 回答 2

3

您不能Styles在相同的ResourceDictionary. 因此,如果您已经为特定类型定义了一个Style没有x:Keyin的隐ResourceDictionary式,则不能将另一个添加到相同的ResourceDictionary.

否则,您应该能够基于Style范围内的默认样式,如下所示:

<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
    <Style.Triggers>

    </Style.Triggers>
</Style>
于 2017-05-19T10:00:14.987 回答
0

请对数据网格内的文本框使用以下内容:

<Style TargetType="TextBox" BasedOn="{StaticResource <your style name>}">

PS:在你的情况下是 TextBoxStyleBase 。

于 2017-05-19T08:53:25.163 回答