是否有一种在XAML WPF中实现祖先样式触发器的简洁方法

时间:2013-04-10 06:38:52

标签: css wpf stylesheet

我正在为基本属性网格创建一些样式。例如,XAML将是

<StackPanel Style="{StaticResource propertyGrid}" Orientation="Vertical"  >

    <ItemsControl Tag="property">
        <Label>Nodes</Label>
        <TextBox Text="{Binding Nodes}"/>
    </ItemsControl>

    <ItemsControl Tag="property">
        <Label >Major Diameter</Label>
        <TextBox Text="{Binding MajorDiameter}"/>
    </ItemsControl>

    <ItemsControl Tag="property">
        <Label>Minor Diameter</Label>
        <TextBox Text="{Binding MinorDiameter}"/>
    </ItemsControl>

    <ItemsControl Tag="property">
        <Label>Excenter</Label>
        <TextBox Text="{Binding Excenter}"> </TextBox>
    </ItemsControl>

</StackPanel>

我的造型遵循这个逻辑。内的标签或文本框 带有标记property的ItemsControl获得特殊样式。如果我 我正在写这个psuedo CSS我写的

ItemsControl.property Label {
    Grid.Row: 0;
    FontWeight: bold;
    Padding:0,4,0,0;
}

ItemsControl.property TextBox {
    Grid.Row: 1;
    FontWeight: bold;
}
在咬牙切齿之后,我想出了一种方法来做到这一点 是使用DataTriggers来查找树而不是树 向下看树的CSS心态。但是我很害怕 由它的冗长。见下文。

<Style TargetType="StackPanel" x:Key="propertyGrid">
    <Style.Resources>

        <Style TargetType="Label">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=Tag}" Value="property">
                    <Setter Property="Grid.Row" Value="0"/>
                    <Setter Property="FontWeight" Value="Bold"/>
                    <Setter Property="Padding" Value="0,4,0,0"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <Style TargetType="TextBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=Tag}" Value="property">
                    <Setter Property="FontWeight" Value="Bold"/>
                    <Setter Property="Grid.Row" Value="1"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <Style TargetType="ItemsControl" x:Key="property">
            <Style.Triggers>
                <Trigger Property="Tag" Value="property">
                    <Setter Property="Focusable" Value="False"/>
                    <Setter Property="ItemsPanel">
                        <Setter.Value>
                            <ItemsPanelTemplate>
                                <Grid Width="Auto">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="40*" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition />
                                        <RowDefinition />
                                    </Grid.RowDefinitions>
                                </Grid>
                            </ItemsPanelTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Style.Resources>
</Style>

我的问题是。这样做是否有捷径或更好的符号?我非常 试图写一个WPFCSS编译器来处理这个问题;)我可以 写一个MarkupExtension来清理它。我希望任何解决方案都有效 在设计时也是如此。

例如,是否可以编写扩展名,例如

<AncestorTrigger TargetType="ItemsControl" Path="Tag" Value="property">
    <Setter Property="Grid.Row" Value="0"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="Padding" Value="0,4,0,0"/>
</AncestorTrigger>

?这比记住如何写

容易得多
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=Tag}" Value="property">

1 个答案:

答案 0 :(得分:0)

我写了一个自定义行为来解决这个问题,它也适用于设计时。标记将

<DataTrigger 
    WPF:WhenParentTag.TargetType="{x:Type ItemsControl}" Value="property">

    <Setter Property="Grid.Row" Value="0"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="Padding" Value="0,4,0,0"/>

</DataTrigger>

有点理智。扩展的代码是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;

namespace My.WPF
{
    public static class WhenParentTag
    {

        static WhenParentTag()
        {}

        public static readonly DependencyProperty 
            TargetTypeProperty = 
            DependencyProperty.RegisterAttached
            ( "TargetType"
            , typeof(Type)
            , typeof(WhenParentTag)
            , new PropertyMetadata(null, TargetTypeChanged));

        public static void 
            TargetTypeChanged
            ( DependencyObject  dob
            , DependencyPropertyChangedEventArgs e)
        {
            var trigger = dob as DataTrigger;
            if(trigger==null){
                return;
            }

            var type = e.NewValue as Type;
            if (type == null)
                return;

            var binding = new Binding();
            var rel = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = type };
            binding.RelativeSource = rel;
            binding.Path = new PropertyPath("Tag");
            trigger.Binding = binding;
        }

        public static Type
            GetTargetType
            ( DataTrigger dp)
        {
            return (Type)dp.GetValue(TargetTypeProperty);
        }

        public static void
            SetTargetType
            ( DataTrigger dp,
            Type value)
        {
            dp.SetValue(TargetTypeProperty, value);
        }
    }
}

欢迎改进建议。