WPF将元素属性的值从BasedOn更改为 [英] WPF Change value of Element's Property from BasedOn

查看:150
本文介绍了WPF将元素属性的值从BasedOn更改为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的风格如下:

<Style TargetType="{x:Type local:MetroTabControl}">
    <Style.Triggers>
        <Trigger Property="SingleRow" Value="True">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:MetroTabControl}">
                        <Grid>
                            <Grid KeyboardNavigation.TabNavigation="Local" SnapsToDevicePixels="True">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <Grid>
                                    <ScrollViewer x:Name="ScrollViewer" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Hidden" Style="{DynamicResource TabPanelScrollViewer}">
                                        <TabPanel x:Name="HeaderPanel" IsItemsHost="True" Panel.ZIndex="1" KeyboardNavigation.TabIndex="1"/>
                                    </ScrollViewer>
                                    <Button x:Name="AddTabItem" Content="&#xE109;"  Style="{DynamicResource TabControlButton}" HorizontalAlignment="Right" VerticalAlignment="Top"/>
                                </Grid>
                                <Border Grid.Row="1" x:Name="TabPanelBorder" Background="Transparent">
                                    <Rectangle x:Name="TabPanelBorderRectangle" Fill="{StaticResource TabPanelBorderBrush}" Height="2"/>
                                </Border>
                                <Border Grid.Row="2" Background="{StaticResource TabControlBackground}"/>
                                <ContentPresenter Grid.Row="2" Name="PART_SelectedContentHost" ContentSource="SelectedContent"/>
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <!--<Setter Property="Template" Value="{StaticResource MetroTabControlSingleRow}" />-->
        </Trigger>
        <Trigger Property="SingleRow" Value="False">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:MetroTabControl}">
                        <Grid KeyboardNavigation.TabNavigation="Local" SnapsToDevicePixels="True">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <Border Background="Transparent" BorderThickness="0,0,0,2"  BorderBrush="{StaticResource TabPanelBorderBrush}">
                                <DockPanel LastChildFill="True">
                                    <Button x:Name="AddTabItem" Style="{DynamicResource TabControlButton}" DockPanel.Dock="Right">
                                        <Path Stroke="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" Data="M0,4 H8 M4,0 V8"  StrokeThickness="2" />
                                    </Button>
                                    <TabPanel x:Name="HeaderPanel" IsItemsHost="True" Panel.ZIndex="1" KeyboardNavigation.TabIndex="1"/>
                                </DockPanel>
                            </Border>
                            <Border Grid.Row="1" Background="{StaticResource TabControlBackground}"/>
                            <ContentPresenter Grid.Row="1" Name="PART_SelectedContentHost" ContentSource="SelectedContent"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <!--<Setter Property="Template" Value="{StaticResource MetroTabControlMultiRows}" />-->
        </Trigger>
    </Style.Triggers>
</Style>

我想使用上面的另一种样式BasedOn,但要进行一次更改-我想更改TabPanelBorderRectangle RectangleFill颜色.

I want to have another style, BasedOn the above, but with one change - I want to change the Fill Color of the TabPanelBorderRectangle Rectangle.

因此,要使用BasedOn,我写了以下内容:

So, to use the BasedOn I wrote the following:

<Style TargetType="{x:Type local:MetroTabControl}" BasedOn="{StaticResource {x:Type local:MetroTabControl}}">

</Style>

但是我不知道如何从BasedOn样式更改TabPanelBorderRectangle Rectangle的颜色.

But I have no Idea how to change the color of the TabPanelBorderRectangle Rectangle from the BasedOn Style.

我尝试过类似的事情

<Setter TargetName="TabPanelBorderRectangle" Property="Fill" Value="Red"/>

但不起作用(无法在样式设置器上设置TargetName属性) ..

but it doesnt work (TargetName property cannot be set on a Style Setter) ..

我该怎么做?

推荐答案

由于错误状态,您不能在样式设置器中使用TargetName.

As error states you cannot use TargetName in style setters.

作为一种替代方法,您可以使用DynamicResource绑定画笔,而不是使用StaticResource进行绑定,以便我们可以利用XAML的资源查找行为.

As a workaround what you can do is, instead of using StaticResource for your brush, bind it using DynamicResource so that we can take advantage of resource look up behaviour of XAML.

<Rectangle x:Name="TabPanelBorderRectangle"
           Fill="{DynamicResource TabPanelBorderBrush}"/>

现在,按照您的样式,您可以通过为画笔指定相同的键并在其中提供颜色值来override that brush.

Now, in your style you can override that brush by specifying the same key for a brush and provide your color value there.

<Style TargetType="{x:Type local:MetroTabControl}"
       BasedOn="{StaticResource {x:Type local:MetroTabControl}}">
    <Style.Resources>
         <SolidColorBrush x:Key="TabPanelBorderBrush" Color="Green"/>
    </Style.Resources>
</Style>

由于brush是通过dynamic resource绑定的,因此它将从您的样式资源中选择most local value,在上述情况下为绿色.

Since the brush is binded via dynamic resource it will pick the most local value from your style resource which will be Green in above case.

这篇关于WPF将元素属性的值从BasedOn更改为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆