Silverlight 4中:如何切换控制可视性 [英] Silverlight 4: how to switch control visibility

查看:145
本文介绍了Silverlight 4中:如何切换控制可视性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Silverlight应用程序使用MVVM。当控制可见性需要得到的数据进行管理,我的'能见度'属性连接对象的相应的属性:

I am using MVVM in my Silverlight app. When control visibility is need to be managed by data, I am connecting its 'Visibility' property to object's corresponding property:

XAML:

<TextBlock Text="Price" Visibility="{Binding PriceVisibility, Mode=OneWay}"/>
<TextBox Text="{Binding TicketPrice, Mode=TwoWay}" Visibility="{Binding PriceVisibility, Mode=OneWay}"/>

codeBehind(C#):

CodeBehind (C#):

public string PriceVisibility { get { return PriceVisible ? "Visible" : "Collapsed"; } }

但是从我的角度来看,返回字符串重新Visibility属性的presentation不是一个最好的办法。

But from my perspective, returning string representation of the Visibility property is not a best approach.

能否请您告知,如果有什么更好的办法?

Could you please advise if there are any better way?

谢谢!

推荐答案

我只是用反射来检查在presentationFramework.dll的类型转换器

I just used Reflector to inspect the type converters in the PresentationFramework.dll

有已经能够布尔和知名度之间进行转换的实现。你应该能够使用这个在您的Silverlight应用程序。

There is already an implementation that can convert between boolean and visibility. You should be able to make use of this in your silverlight application.

public sealed class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool flag = false;
        if (value is bool)
        {
            flag = (bool) value;
        }
        else if (value is bool?)
        {
            bool? nullable = (bool?) value;
            flag = nullable.HasValue ? nullable.Value : false;
        }
        return (flag ? Visibility.Visible : Visibility.Collapsed);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((value is Visibility) && (((Visibility) value) == Visibility.Visible));
    }
}

这篇关于Silverlight 4中:如何切换控制可视性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆