WPF:根据条件将不同的枚举填充到我的 ComBoBox 中
WPF: populate different enum into my ComBoBox base on condition
所以我有 2 个枚举:
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
public enum A400TypeOption
{
[Description("Temperature")]
TEMPERATURE = 0,
[Description("Humidity sensor")]
HUMIDITY = 2,
}
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
public enum A700TypeOption
{
[Description("Temperature")]
TEMPERATURE = 1,
[Description("High")]
HIGH = 2,
[Description("Voltage")]
VOLTAGE = 3,
}
我有这个 EnumBindingSource
class:
public class EnumBindingSourceExtension : MarkupExtension
{
private Type _enumType;
public Type EnumType
{
get { return this._enumType; }
set
{
if (value != this._enumType)
{
if (null != value)
{
Type enumType = Nullable.GetUnderlyingType(value) ?? value;
if (!enumType.IsEnum)
throw new ArgumentException("Type must be for an Enum.");
}
this._enumType = value;
}
}
}
public EnumBindingSourceExtension() { }
public EnumBindingSourceExtension(Type enumType)
{
this.EnumType = enumType;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (null == this._enumType)
throw new InvalidOperationException("The EnumType must be specified.");
Type actualEnumType = Nullable.GetUnderlyingType(this._enumType) ?? this._enumType;
Array enumValues = Enum.GetValues(actualEnumType);
if (actualEnumType == this._enumType)
return enumValues;
Array tempArray = Array.CreateInstance(actualEnumType, enumValues.Length + 1);
enumValues.CopyTo(tempArray, 1);
return tempArray;
}
}
这是我的 ComBoBox
得到它的 enum
:
<ComboBox ItemsSource="{Binding Source={helpers:EnumBindingSource {x:Type tagEnums:A400TypeOption}}}"
</ComboBox>
如您所见,在这个例子中,我的 ComBoBox
填充了 enum
A400TypeOption
。
现在我有另一个 ComBoBox
,用户可以在 A400
和 A700
之间选择,并基于这个 selection 我想填充我的 ComBoBox
与相关的 enum
(A400TypeOption
或 A700TypeOption
)。
所以我在 ViewModel
:
中创建了这个 int value
属性
private int enumType;
public int EnumType
{
get { return enumType; }
set
{
enumType = value;
NotifyPropertyChanged("EnumType");
}
}
如果用户 select A400
并且 EnumType
值为 0
我想以这种方式填充我的 ComBoBox
:
<ComboBox ItemsSource="{Binding Source={helpers:EnumBindingSource {x:Type tagEnums:A400TypeOption}}}"
如果用户 select A700
并且 EnumType
值为 1
我想以这种方式填充我的 ComBoBox
:
<ComboBox ItemsSource="{Binding Source={helpers:EnumBindingSource {x:Type tagEnums:A700TypeOption}}}"
所以我想知道我该怎么做?
我想也许可以创建 converter
并且老实说,我不知道该向 converter
.
传递什么
有什么想法吗?
这样使用:
<ComboBox x:Name="SelectType">
<ComboBoxItem Content="A400" Tag="{Binding Source={helpers:EnumBindingSource {x:Type tagEnums:A400TypeOption}}}" />
<ComboBoxItem Content="A700" Tag="{Binding Source={helpers:EnumBindingSource {x:Type tagEnums:A700TypeOption}}}" />
</ComboBox>
<ComboBox ItemsSource="{Binding SelectedItem.Tag, ElementName=SelectType}" />
所以我有 2 个枚举:
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
public enum A400TypeOption
{
[Description("Temperature")]
TEMPERATURE = 0,
[Description("Humidity sensor")]
HUMIDITY = 2,
}
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
public enum A700TypeOption
{
[Description("Temperature")]
TEMPERATURE = 1,
[Description("High")]
HIGH = 2,
[Description("Voltage")]
VOLTAGE = 3,
}
我有这个 EnumBindingSource
class:
public class EnumBindingSourceExtension : MarkupExtension
{
private Type _enumType;
public Type EnumType
{
get { return this._enumType; }
set
{
if (value != this._enumType)
{
if (null != value)
{
Type enumType = Nullable.GetUnderlyingType(value) ?? value;
if (!enumType.IsEnum)
throw new ArgumentException("Type must be for an Enum.");
}
this._enumType = value;
}
}
}
public EnumBindingSourceExtension() { }
public EnumBindingSourceExtension(Type enumType)
{
this.EnumType = enumType;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (null == this._enumType)
throw new InvalidOperationException("The EnumType must be specified.");
Type actualEnumType = Nullable.GetUnderlyingType(this._enumType) ?? this._enumType;
Array enumValues = Enum.GetValues(actualEnumType);
if (actualEnumType == this._enumType)
return enumValues;
Array tempArray = Array.CreateInstance(actualEnumType, enumValues.Length + 1);
enumValues.CopyTo(tempArray, 1);
return tempArray;
}
}
这是我的 ComBoBox
得到它的 enum
:
<ComboBox ItemsSource="{Binding Source={helpers:EnumBindingSource {x:Type tagEnums:A400TypeOption}}}"
</ComboBox>
如您所见,在这个例子中,我的 ComBoBox
填充了 enum
A400TypeOption
。
现在我有另一个 ComBoBox
,用户可以在 A400
和 A700
之间选择,并基于这个 selection 我想填充我的 ComBoBox
与相关的 enum
(A400TypeOption
或 A700TypeOption
)。
所以我在 ViewModel
:
int value
属性
private int enumType;
public int EnumType
{
get { return enumType; }
set
{
enumType = value;
NotifyPropertyChanged("EnumType");
}
}
如果用户 select A400
并且 EnumType
值为 0
我想以这种方式填充我的 ComBoBox
:
<ComboBox ItemsSource="{Binding Source={helpers:EnumBindingSource {x:Type tagEnums:A400TypeOption}}}"
如果用户 select A700
并且 EnumType
值为 1
我想以这种方式填充我的 ComBoBox
:
<ComboBox ItemsSource="{Binding Source={helpers:EnumBindingSource {x:Type tagEnums:A700TypeOption}}}"
所以我想知道我该怎么做?
我想也许可以创建 converter
并且老实说,我不知道该向 converter
.
有什么想法吗?
这样使用:
<ComboBox x:Name="SelectType">
<ComboBoxItem Content="A400" Tag="{Binding Source={helpers:EnumBindingSource {x:Type tagEnums:A400TypeOption}}}" />
<ComboBoxItem Content="A700" Tag="{Binding Source={helpers:EnumBindingSource {x:Type tagEnums:A700TypeOption}}}" />
</ComboBox>
<ComboBox ItemsSource="{Binding SelectedItem.Tag, ElementName=SelectType}" />