根据枚举值启用文本框 [英] Enable TextBox depending on enum value
问题描述
我有我想启用一个TextBox如果订单的
状态要么是 OrderStatus.New
或 OrderStatus.Ordered
。这是其它东西,文本框应保持关闭。
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"/>
我想我需要使用某种MultiBinding,但似乎无法找到如何做,在这种特殊情况下适当的资源。
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.
推荐答案
您应该使用ValueConverter为此:
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();
}
}
然后用它作为你的XAML转换器:
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 上宣布的资源。
参数化
一个单一的转换器可进行参数化,因此它可以为不同的订单类型被重用。
该XAML会是这样:
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.
这阵列可以被送入 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();
}
}
这篇关于根据枚举值启用文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!