我有以下情况:
问题:无法将属性作为枚举绑定到wpf mvvm中的combox?
如何将此枚举绑定到组合框?
1.我有一个枚举。
public enum RankType
{
StringValue1,
StringValue2,
StringValue3
}
2.我在myclass中有一个enum属性:
[DefaultValue(RankType.StringValue1)]
[ConvertUsing(typeof(EnumTypeConverter<RankType>))]
public RankType Rank { set; get; }
ConvertUsing Class,它为我做转换:
[System.AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ConvertUsingAttribute:Attribute
{
private TypeConverter _converter = null;
public TypeConverter Converter
{
get
{
if (_converter == null)
_converter = (TypeConverter)System.Activator.CreateInstance(TypeOfConverter);
return _converter;
}
}
public Type TypeOfConverter
{
get;
private set;
}
public ConvertUsingAttribute(Type converterType)
{
this.TypeOfConverter = converterType;
}
}
EnumTypeConverter类:
public class EnumTypeConverter<T>:TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value != null && value is string)
{
return Enum.Parse(typeof(T), (string)value);// int.Parse(((string)value).Trim());
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (value != null && destinationType == typeof(string))
{
return ((T)value).ToString();
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
4.在ViewModel中
private MilitaryRankType _selectedRankType;
public RankType SelectedRankType
{
get { return _selectedRankType; }
set
{
_selectedRankType = value;
NotifyPropertyChanged(nameof(RankTypes));
}
}
private RankType[] _rankTypes;
public RankType[] RankTypes
{
get
{
return _rankTypes ??
(_rankTypes =Enum.GetValues(typeof(RankType)).Cast<RankType>().ToArray());
}
}
5.在视图中
<ComboBox ItemsSource="{Binding RankTypes, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" SelectedItem="{Binding SelectedRankType}" />
6.我使用了xaml中的listview
<ListView ItemsSource="{Binding EmployeesList}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="3">
<ListView.View>
<GridView>
<GridViewColumn Width="150" Header="">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=NationalId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="150" Header="">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=CardId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="150" Header="">
<GridViewColumn.CellTemplate>
<DataTemplate>
**<ComboBox SelectedItem="{Binding SelectedRankType}" />**
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="150" Header="">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="150" Header="">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=LastName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,NotifyOnValidationError=True,ValidatesOnDataErrors=True,ValidatesOnExceptions=True}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
答案 0 :(得分:0)
老实说,我真的不知道你的代码出了什么问题,但我可以给你一个工作的例子,没有任何花哨的转换器东西:
假设您有以下窗口内容:
<Grid x:Name="grid1">
<ComboBox ItemsSource="{Binding SelectableRanks}" SelectedItem="{Binding SelectedRank}"/>
</Grid>
并在窗口构造函数中初始化datacontext:
public MainWindow()
{
InitializeComponent();
grid1.DataContext = new RankSelectionVM();
}
这是你的viewmodel:
public class RankSelectionVM
{
private RankType _SelectedRank;
public RankType SelectedRank
{
get { return _SelectedRank; }
set { _SelectedRank = value; }
}
private RankType[] _rankTypes;
public RankType[] SelectableRanks
{
get
{
return _rankTypes ??
(_rankTypes = Enum.GetValues(typeof(RankType)).Cast<RankType>().ToArray());
}
}
}
public enum RankType
{
StringValue1,
StringValue2,
StringValue3
}
它没有附加任何INotifyPropertyChanged
或任何转换器,但在SelectedRank
setter中放置了一个断点 - 当你选择一个值时它应该会中断。如果此代码适合您,您必须找到不同的方法,以便在项目中获得无效代码。否则你可能会有一些非常奇怪的问题需要特别小心。