2

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.

Problem I 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.

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

<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

<Path Fill="{Binding ElementName=changeLbl,Path=Foreground}" Stroke="{Binding ElementName=changeLbl,Path=Foreground}" Style="{StaticResource changePathTrigger}"/>
4

1 回答 1

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直接依赖于属性“更改”。然后,例如,您可以更改您的PathTextBlock颜色并且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>
于 2013-07-29T20:36:41.910 回答