收录日期:2020/10/28 11:06:30 时间:2010-09-07 01:31:00 标签:silverlight,mvvm,binding,silverlight-4.0,visibility

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#):

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

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?

Thanks!

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

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));
    }
}

I've faced the problem of binding a Boolean value to the visibility property, so I've implemented my own Boolean to Visibility Converter, I'm using it with most of my applications.

Add the Following Class to your application:

public class BoolVisibilityConverter : IValueConverter{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture){
        bool isVisible = (bool)value;
        return isVisible ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
    }

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

Now To Use it you'll need to add it as a resource in your XAML Code.

<UserControl.Resources>
    <Helpers:BoolVisibilityConverter x:Key="boolVisibilityConverter" />
</UserControl.Resources>

In your example use the following:

<TextBlock Text="Price" Visibility="{Binding PriceVisibility, Mode=OneWay, Converter={StaticResource boolVisibilityConverter}}"/>

<TextBox Text="{Binding TicketPrice, Mode=TwoWay}" Visibility="{Binding PriceVisibility, Mode=OneWay, Converter={StaticResource boolVisibilityConverter}}"/>