سؤال

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?

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top