Silverlight 数据影响外观.. VisualStateManger .. 根据数据值的颜色

发布于 2024-10-06 20:19:35 字数 126 浏览 2 评论 0原文

在Silverlight中,如何让按钮的颜色根据其内容的值而改变..例如'0'=红色,'1'=绿色..

我已经查看了VisualStateManger但不知道该怎么做它..我可以看到鼠标悬停等很容易..但对于数据值则不然。

In Silverlight how do I get the color of a button to change according to the value of its contents .. e.g. '0' = red , '1' = green ..

I have taken a look at the VisualStateManger but cannot see how to do it .. I can see it is easy for mouseovers etc .. but not for values of data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红墙和绿瓦 2024-10-13 20:19:35

您需要的是一个值转换器,即 IValueConverter 的实现。在这篇博客文章中,您找到可用于您的任务的 StringToObjectConverter 代码。我将在此处重现代码:-

using System;
using System.Windows;
using System.Windows.Data;
using System.Linq;
using System.Windows.Markup;

namespace SilverlightApplication1
{
    [ContentProperty("Items")]
    public class StringToObjectConverter : IValueConverter
    {
        public ResourceDictionary Items { get; set; }
        public string DefaultKey { get; set; }

        public StringToObjectConverter()
        {
            DefaultKey = "__default__";
        }

        public virtual object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && Items.Contains(value.ToString()))
                return Items[value.ToString()];
            else
                return Items[DefaultKey];
        }

        public virtual object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Items.FirstOrDefault(kvp => value.Equals(kvp.Value)).Key;
        }
    }
}

现在您可以将此转换器的实例添加到用户控件中的资源中:-

 <UserControl.Resources>
     <local:StringToObjectConverter x:Key="StatusToBrush">
         <ResourceDictionary>
             <SolidColorBrush Color="Red" x:Key="0" />
             <SolidColorBrush Color="Green" x:Key="1" />
             <SolidColorBrush Color="Silver" x:Key="__default__" /> 
         </ResourceDictionary>
     </local:StringToObjectConverter>
 </UserControl>

现在您可以将 Background 绑定到您的值:-

 <Button Background="{Binding Value, Converter={StaticResource StatusToBrush}}">
    <TextBlock Text="{Binding Value}" />
 </Button>

What you need is a value converter, that is an implementation of IValueConverter. In this blog article you find the code for a StringToObjectConverter which you can use for your task. I'll reproduce the code here:-

using System;
using System.Windows;
using System.Windows.Data;
using System.Linq;
using System.Windows.Markup;

namespace SilverlightApplication1
{
    [ContentProperty("Items")]
    public class StringToObjectConverter : IValueConverter
    {
        public ResourceDictionary Items { get; set; }
        public string DefaultKey { get; set; }

        public StringToObjectConverter()
        {
            DefaultKey = "__default__";
        }

        public virtual object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && Items.Contains(value.ToString()))
                return Items[value.ToString()];
            else
                return Items[DefaultKey];
        }

        public virtual object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Items.FirstOrDefault(kvp => value.Equals(kvp.Value)).Key;
        }
    }
}

Now you can add an instance of this converter to the resources in you your user control:-

 <UserControl.Resources>
     <local:StringToObjectConverter x:Key="StatusToBrush">
         <ResourceDictionary>
             <SolidColorBrush Color="Red" x:Key="0" />
             <SolidColorBrush Color="Green" x:Key="1" />
             <SolidColorBrush Color="Silver" x:Key="__default__" /> 
         </ResourceDictionary>
     </local:StringToObjectConverter>
 </UserControl>

Now you can bind the Background to your value:-

 <Button Background="{Binding Value, Converter={StaticResource StatusToBrush}}">
    <TextBlock Text="{Binding Value}" />
 </Button>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文