Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

xaml - WPF Change value of Element's Property from BasedOn

I have the following style:

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

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

So, to use the BasedOn I wrote the following:

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

</Style>

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

I tried something like

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

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

How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As error states you cannot use TargetName in style setters.

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}"/>

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>

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...