WPF Style BasedOn parent Style in current context

こ雲淡風輕ζ 提交于 2019-12-23 03:32:00

问题


Say, I have a default style for a TextBox 'TextBoxStyleBase'. I then define a DataGrid style which has an own TextBox style BasedOn that Base-style, defining another Border Color.

In some place inside a DataGrid I want to define another TextBox style but inherit from the one defined in DataGrid style.

Is there a way to make a style inherit from the style that is currently defined for a specific control in the current 'context'?

EDIT:

To make it more clear, here's what I have:

<!-- 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>

What to put in BasedOn = '???' so that the text shows up in FontSize 20 but Bold if hovered.


回答1:


You cannot add two Styles with the same key inside the same ResourceDictionary. So if you already have defined an implicit Style without an x:Key in a ResourceDictionary for a specific type, you cannot add another one to the same ResourceDictionary.

Otherwise you should be able to base a Style on the default style that is in scope like this:

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

    </Style.Triggers>
</Style>



回答2:


Please use the following for the textbox inside the datagrid:

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

PS: would be TextBoxStyleBase in your case.



来源:https://stackoverflow.com/questions/44065383/wpf-style-basedon-parent-style-in-current-context

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!