Enable TextBox depending on enum value

只谈情不闲聊 提交于 2019-12-13 04:36:44

问题


I have a TextBox that I want to enable if a Order's Status is either OrderStatus.New or OrderStatus.Ordered. It it's something else, the TextBox should stay disabled.

<TextBox Text="{Binding OrderedAmount}" IsEnabled="True"/>

I assume I need to use some kind of MultiBinding, but cannot seem to find a proper resource on how to do that in this particular case.


回答1:


You should use a ValueConverter for this:

public class IsNewOrOrderedConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        OrderStatus status = (OrderStatus)value;
        return status == OrderStatus.New || status == OrderStatus.Ordered;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Then use it as the converter in your xaml:

<TextBox Text="{Binding OrderedAmount}" 
          IsEnabled="{Binding OrderStatus, Converter={StaticResource IsNewOrOrderedConverter}"/>

Don't forget to declare the resource:

 <App.Resources>
    <myPrefix:IsNewOrOrderedConverter x:Key="IsNewOrOrderedConverter" />
 </App.Resources>

http://msdn.microsoft.com/en-us/library/ms750613.aspx on declaring resources.

Parametrization

A single converter can be made parametrized so it can be reused for different order types. The XAML would be like this:

        <local:OrderStatusToBooleanConverter 
               StatusList="New,Ordered"  x:Key="NewOrOrderedConverter" />
        <local:OrderStatusToBooleanConverter 
               StatusList="Delivered"  x:Key="DeliveredConverter" />

This requires some special tactics since there is no way by default to make it readable (with enum values separated by a comma). That's where we need a type converter:

public class StringToOrderStatusArrayConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(string);
    }
    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        if (value == null)
        {
            return new OrderStatus[0];
        }
        else
        {
            return (from s in value.ToString().Split(',')
                    select Enum.Parse(typeof(OrderStatus), s))
                    .OfType<OrderStatus>()
                    .ToArray<OrderStatus>();

        }
    }
}

The type converter converts the input string array of enum values separated by a comma into an array.

This array can then be fed into the ValueConverter:

public class OrderStatusToBooleanConverter : IValueConverter
{
    [TypeConverter(typeof(StringToOrderStatusArrayConverter))]
    public OrderStatus[] StatusList { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        OrderStatus status = (OrderStatus)value;
        return StatusList != null && StatusList.Contains(status);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}


来源:https://stackoverflow.com/questions/16320781/enable-textbox-depending-on-enum-value

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