WPF 路径对象中的属性或数据触发器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17933431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Property or Data Trigger in WPF Path object
提问by Nair
I have a TextBlock and two path objects (Up arrow & Down arrow) in a Stack Panel. The TextBlock is bound to a Property called 'Change', which possibly can have values such as -1,0,1.
我在堆栈面板中有一个 TextBlock 和两个路径对象(向上箭头和向下箭头)。TextBlock 绑定到名为“Change”的属性,该属性可能具有诸如 -1,0,1 之类的值。
ProblemI wanted to show Up arrow in case of 1 OR Down arrow in case of -1 OR show neither of these Path object in case of 0.
问题我想在 1 的情况下显示向上箭头,或者在 -1 的情况下显示向下箭头,或者在 0 的情况下不显示这些 Path 对象。
In order to achieve this, I defined a Style Trigger on Path type.
为了实现这一点,我在路径类型上定义了一个样式触发器。
Question
题
I am not able to get it work. Am i missing something or is there better way of doing this.
我无法让它工作。我是否遗漏了什么或者有更好的方法来做到这一点。
XAML
XAML
<StackPanel Orientation="Horizontal">
<TextBlock Name="changeLbl" Margin="0,0,10,0" Text="{Binding Path=Change}"/>
<Path Fill="{Binding ElementName=changeLbl,Path=Foreground}" Style="{StaticResource downChangePathTrigger}"/>
<Path Fill="{Binding ElementName=changeLbl,Path=Foreground}" Style="{StaticResource upChangePathTrigger}"/>
</StackPanel>
Style resource
风格资源
<Style x:Key="upChangePathTrigger" TargetType="Path">
<Setter Property="Visibility" Value="Collapsed" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Data" Value="M 0 4 L 4 0 L 8 4 Z" />
<Setter Property="StrokeThickness" Value="5" />
<Setter Property="Stroke" Value="GreenYellow"/>
<Style.Triggers>
<Trigger Property="Fill" Value="GreenYellow">
<Setter Property="Visibility" Value="Visible"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="downChangePathTrigger" TargetType="Path">
<Setter Property="Visibility" Value="Collapsed" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Data" Value="M 0 0 L 4 4 L 8 0 Z" />
<Setter Property="StrokeThickness" Value="5" />
<Setter Property="Stroke" Value="Red"/>
<Style.Triggers>
<Trigger Property="Fill" Value="Red">
<Setter Property="Visibility" Value="Visible"/>
</Trigger>
</Style.Triggers>
</Style>
Update with answer
更新答案
Style trigger
风格触发
<Style x:Key="changePathTrigger" TargetType="Path">
<Setter Property="Visibility" Value="Hidden" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="StrokeThickness" Value="5" />
<Style.Triggers>
<!--<DataTrigger Binding="{Binding Path=Change,Converter={StaticResource doubleToIntConverter}}" Value="0">
<Setter Property="Stroke" Value="0"/>
</DataTrigger>-->
<DataTrigger Binding="{Binding Path=Change,Converter={StaticResource doubleToIntConverter}}" Value="1">
<Setter Property="Visibility" Value="Visible"/>
<Setter Property="Data" Value="M 0 4 L 4 0 L 8 4 Z"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Change,Converter={StaticResource doubleToIntConverter}}" Value="-1">
<Setter Property="Visibility" Value="Visible"/>
<Setter Property="Data" Value="M 0 0 L 4 4 L 8 0 Z"/>
</DataTrigger>
</Style.Triggers>
</Style>
Xaml
xml
<Path Fill="{Binding ElementName=changeLbl,Path=Foreground}" Stroke="{Binding ElementName=changeLbl,Path=Foreground}" Style="{StaticResource changePathTrigger}"/>
回答by LPL
It is a strange construction (trigger Path.Visibilitywith Path.Fillcolor which is bound to TextBlock.Foreground) but it will work with
这是一个奇怪的结构(Path.Visibility带有Path.Fill绑定到的颜色的触发器TextBlock.Foreground)但它可以与
<TextBlock Name="changeLbl" Margin="0,0,10,0" Text="{Binding Path=Change}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Text" Value="1">
<Setter Property="Foreground" Value="GreenYellow" />
</Trigger>
<Trigger Property="Text" Value="-1">
<Setter Property="Foreground" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
But I think you should reduce the dependencies e.g. replace Triggerin path style with a DataTriggerto depend from property 'Change' directly. Then for example you can change your Pathor TextBlockcolors and Visibilityis still working.
但是我认为您应该减少依赖项,例如将Trigger路径样式替换为DataTrigger直接依赖于属性“更改”。然后例如您可以更改您的Path或TextBlock颜色并且Visibility仍在工作。
<Style x:Key="upChangePathTrigger" TargetType="Path">
...
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Change}" Value="1">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>

