Silverlight 4: how to switch control visibility

浪尽此生 提交于 2019-12-18 04:14:48

问题


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!


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/3655210/silverlight-4-how-to-switch-control-visibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!