【问题标题】:WPF Change value of Element's Property from BasedOnWPF 从 BasedOn 更改元素属性的值
【发布时间】:2013-11-30 17:21:05
【问题描述】:

我有以下风格:

<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 Rectangle 的 Fill 颜色。

所以,为了使用 BasedOn,我写了以下内容:

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

</Style>

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

我尝试了类似的东西

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

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

我该怎么做?

【问题讨论】:

    标签: wpf xaml styles basedon


    【解决方案1】:

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

    作为一种解决方法,您可以做的是,不要使用 StaticResource 作为画笔,而是使用 DynamicResource 绑定它,以便我们可以利用 XAML 的资源查找行为。

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

    现在,您可以按照自己的风格override that brush,通过为画笔指定相同的键并在那里提供您的颜色值。

    <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,在上述情况下为绿色。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-11
      相关资源
      最近更新 更多