gpt4 book ai didi

css - 在 XAML WPF 中是否有一种干净的方法来实现祖先样式触发器

转载 作者:太空宇宙 更新时间:2023-11-03 18:47:36 25 4
gpt4 key购买 nike

我正在为基本属性网格创建一些样式。示例的 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 获得特殊样式。如果我这样做就像我写的伪 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">

最佳答案

我编写了一个自定义行为来解决这个在设计时也有效的问题。标记将

<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);
}
}
}

欢迎提出改进建议。

关于css - 在 XAML WPF 中是否有一种干净的方法来实现祖先样式触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15918681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com