如何在Silverlight中更改Style.Triggers

时间:2014-10-28 16:08:48

标签: xaml silverlight

我是SilverLight的初学者,在下面的xaml代码中,Silverlight无法识别Style.Triggers,我对此进行了评论,在这种情况下,Style.Triggers的替代是什么?如果是VisualStateManager,我该如何将其更改为VisualStateManager

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Setter Property="IsEnabled" Value="False"/>
        <!--<Style.Triggers>
            <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                <Setter Property="Control.Background" Value="#d9e2ea"/>
            </Trigger>
            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                <Setter Property="Control.Background" Value="#9eceec"/>
            </Trigger>
        </Style.Triggers>-->
    </Style>
</ListBox.ItemContainerStyle>

1 个答案:

答案 0 :(得分:0)

我认为,您需要根据布尔值更改背景颜色。

您可以创建并使用'BooleanToBrushConverter'

C#代码:

public class BooleanToBrushConverter : IValueConverter
{
    public Brush TrueBrush { get; set; }

    public Brush FalseBrush { get; set; }

    public Brush NullBrush { get; set; }

    /// <summary>
    /// Converts the data for display.
    /// </summary>
    public Object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        if (value == null) return NullBrush;
        else return (bool)value ? TrueBrush : FalseBrush;
    }

    /// <summary>
    /// Converts the data from display
    /// </summary>
    public Object ConvertBack(Object value, Type targetType, Object parameter,
        System.Globalization.CultureInfo culture)
    {
        return value == TrueBrush;
    }
}

XAML代码:

(您需要将其包含在XAML的顶部) xmlns:lc =“clr-namespace:NamespaceOfYourConverterClass”

(在转换器内添加以下代码&lt; Grid.Resources&gt;&lt; /Grid.Resources> of main&lt; Grid&gt;)

            <lc:BooleanToBrushConverter x:Key="myBooleanToColorConverter"
                                       TrueBrush="#9eceec"
                                       FalseBrush="#d9e2ea" />

(现在,您需要在绑定控件的Background属性时使用此'Converter')

喜欢:

<ItemsControl Background="{Binding IsActive, Converter={StaticResource myBooleanToColorConverter}}">
</ItemsControl>

注意:“IsActive”是ViewModel中的布尔属性,返回true(1)或false(0)