简体   繁体   English

Silverlight中的条件样式?

[英]Conditional Styling In Silverlight?

While I'm fine with standard control styling in silverlight I have recently began using more dynamic methods of fetching data to be displayed in items controls. 虽然我对Silverlight中的标准控件样式很好,但我最近开始使用更多动态方法来获取要在项目控件中显示的数据。 One of the controls I am reworking is a collection of links. 我正在重做的控件之一是链接集合。

The issue I am having is that each link is coloured differently when moused over. 我遇到的问题是,当鼠标悬停时,每个链接的颜色都不同。 One red, one blue, one green, etc. Is there a way to style these items without sacrificing the dynamics of using an items control with a data template? 一个红色,一个蓝色,一个绿色等。有没有一种方法来设置这些项目的样式而不牺牲使用数据模板的项目控件的动态?

I have done this using a simple converter on a property of the view model, for example lets say you had a boolean property that you wanted to control a style you could do this. 我在视图模型的属性上使用一个简单的转换器完成了这个操作,例如假设您有一个布尔属性,您想要控制一个可以执行此操作的样式。

public class BoolToStyleConverter : IValueConverter
{
    public Style TrueStyle{ get; set; }
    public Style FalseStyle{ get; set; }
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((bool)value) ? TrueStyle : FalseStyle;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

then as a resource you would define your two styles... 然后作为资源,你将定义你的两种风格......

    <common:BoolToStyleConverter x:Key="BoldTextConverter">
        <common:BoolToStyleConverter.TrueStyle>
            <Style TargetType="TextBlock">
                <Setter Property="FontWeight"
                        Value="Bold"></Setter>
            </Style>
        </common:BoolToStyleConverter.TrueStyle>
        <common:BoolToStyleConverter.FalseStyle>
            <Style TargetType="TextBlock">
                <Setter Property="FontWeight"
                        Value="Normal"></Setter>
            </Style>
        </common:BoolToStyleConverter.FalseStyle>
    </common:BoolToStyleConverter>

then you would apply it to your object like this... 然后你会像这样将它应用到你的对象......

<TextBlock Text="{Binding Description}"
           Margin="20,4,4,4"
           Style="{Binding IsConfirmed, Converter={StaticResource BoldTextConverter}}"></TextBlock>

Where IsConfirmed is a boolean property on the viewmodel, this will also keep the style in sync if the IsConfirmed property changes. 其中IsConfirmed是viewmodel上的布尔属性,如果IsConfirmed属性更改,这也将使样式保持同步。

If you want to use a more complicated condition than a Boolean you could always create a Dictionary of objects to Styles in your converter and then have the converter do a lookup, but i have found that usually booleans work in most cases. 如果你想使用一个比布尔更复杂的条件,你总是可以在转换器中创建一个对象字典到样式,然后让转换器进行查找,但我发现通常布尔值在大多数情况下都有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM