0

这是我想做的伪代码

IF NOT ISDIRTY
    THEN VISIBILITY EQ VISIBILITY.COLLAPSED

IF ISDIRTY AND ISVALID        
    THEN VISIBILITY EQ VISIBILITY.VISIBLE AND COLOR = GREEN

IF ISDIRTY AND NOT ISVALID        
    THEN VISIBILITY EQ VISIBILITY.VISIBLE AND COLOR = RED

下面的 ToggleButton 的样式在第一次更新时获得正确的条件,但此后不会改变。

例如,如果我进行了不会使 vm 无效的更改,则颜色正确为绿色。但是,如果我随后进行更改以使 vm 无效,它会保持绿色,而它应该是红色的。相反,如果第一次更新使 vm 无效,则颜色正确设置为红色,但纠正无效错误但使 vm 变脏的第二次更新不会将颜色更改为绿色。

我究竟做错了什么?

我还没有尝试过 Visual State Manager,但这会是首选方式吗?看起来怎么样?

用法

        <Ellipse Style="{StaticResource EditedStateIndicatorStyle}"/>

样式

<Style x:Key="EditedStateIndicatorStyle" TargetType="{x:Type Ellipse}">
    <Setter Property="Width" Value="8" />
    <Setter Property="Height" Value="8" />
    <Setter Property="Margin" Value="8,0"/>
    <Setter Property="SnapsToDevicePixels" Value="false" />
    <Setter Property="Focusable" Value="False" />

    <Style.Triggers>

        <!-- Dirty, && NOT Valid -->
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsDirty}" Value="true"/>
                <Condition Binding="{Binding IsValid}" Value="false"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Fill" Value="Red"/>
            <Setter Property="ToolTip" Value="You got errors, fool!"/>
        </MultiDataTrigger>

        <!-- Dirty, but Valid -->
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsDirty}" Value="true"/>
                <Condition Binding="{Binding IsValid}" Value="true"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Fill" Value="Green"/>
            <Setter Property="ToolTip" Value="You made changes!"/>
        </MultiDataTrigger>

        <!-- Not Dirty, don't show anything -->
        <DataTrigger Binding="{Binding IsDirty}" Value="false">
            <Setter Property="Visibility" Value="Collapsed"/>
        </DataTrigger>
    </Style.Triggers>

</Style>
4

2 回答 2

2

如果您的 ViewModel 正在实施,请INotifyPropertyChanged确保它正确地引发了事件。说我推荐一个简化:

<Style TargetType="{x:Type Ellipse}">
    ...
    <Setter Property="Fill" Value="Green" />
    <Setter Property="ToolTip" Value="You made changes!" />

    <Style.Triggers>
        <!-- Dirty, but NOT Valid -->
        <DataTrigger Binding="{Binding IsValid}" Value="false">
            <Setter Property="Fill" Value="Red" />
            <Setter Property="ToolTip" Value="You got errors, fool!" />
        </DataTrigger>

        <!-- Not Dirty, don't show anything -->
        <DataTrigger Binding="{Binding IsDirty}" Value="false">
            <Setter Property="Visibility" Value="Collapsed"/>
        </DataTrigger>
    </Style.Triggers>
</Style>
于 2012-06-29T15:13:39.700 回答
1

尝试将填充设置为默认颜色(绿色)作为触发器外部的设置器(即在样式中)

于 2012-06-29T14:52:33.073 回答