假设我有以下类层次结构:
public static class Constants
{
public enum MyEnum
{
Value1 =0,
Value2,
Value3
}
}
public class Data : INotifyPropertyChanged
{
public Data(string name, ushort id, Constants.MyEnum e)
{
DataName = name;
DataId = id;
DataEnum = e;
}
#region Properties
// get / set implementation not shown
public string DataName;
public ushort DataId;
public Constants.MyEnum DataEnum;
#endregion
// INotifyPropertyChanged implementation not shown
// Fields implementation not shown
}
public class DataContainer
{
public DataContainer()
{
ContainedData = new ObservableCollection<Data>();
ContainedData.Add(new Data("data1", 1, Constants.MyEnum.Value1));
ContainedData.Add(new Data("data2", 2, Constants.MyEnum.Value2));
ContainedData.Add(new Data("data3", 3, Constants.MyEnum.Value3));
}
public ObservableCollection<Data> ContainedData;
}
我想将DataContainer的ContainedData数据绑定到ListView并创建一个包含以下内容的ItemTemplate:
我的目标:
问题:
答案 0 :(得分:5)
要将MyEnum值转换为ItemsControl(如ComboBox),请参阅http://blogs.msdn.com/wpfsdk/archive/2007/02/22/displaying-enum-values-using-data-binding.aspx。要在ListView中的DataTemplate中显示它,您将使用CellTemplate属性:
<DataTemplate x:Key="DataEnumTemplate">
<ComboBox ItemsSource="..." SelectedItem="{Binding DataEnum, Mode=TwoWay}" />
</DataTemplate>
<GridViewColumn CellTemplate="{StaticResource DataEnumTemplate" />
(其中ItemsSource是根据链接的文章)。
重新数据类型,TextBox.Text绑定将自动在文本字符串和ushort之间进行转换,如果字符串不可转换(例如“abc”),它将自动发出验证错误信号。