views:

158

answers:

2

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!

+2  A: 

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));
    }
}
Rohan West
Thank you for example. Do you know why ReSharper tells: "nullable.HasValue" is always true?
Budda
+2  A: 

Hello, 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}}"/>

Hope that helps :)

Regards,
Monir Abu Hilal
twitter: @MonirAbuHilal

Monir Abu Hilal
Thank you for FULL example (I wasn't aware about XAML markup too).
Budda