我在堆叠面板中有一个TextBlock和两个路径对象(向上箭头和向下箭头)。 TextBlock绑定到名为“Change”的属性,该属性可能包含诸如-1,0,1。
之类的值问题 我想在1或者向下箭头的情况下显示向上箭头,如果是-1,则在0的情况下不显示这些Path对象。
为了实现这一点,我在路径类型上定义了一个样式触发器。
问题
我无法让它发挥作用。我错过了什么,或者有更好的方法。
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 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>
以答案更新
样式触发器
<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
<Path Fill="{Binding ElementName=changeLbl,Path=Foreground}" Stroke="{Binding ElementName=changeLbl,Path=Foreground}" Style="{StaticResource changePathTrigger}"/>
答案 0 :(得分:4)
这是一个奇怪的构造(触发器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>
但我认为你应该减少依赖关系,例如使用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>