[英]WPF ListView ItemTemplate questions
Let's say that I've got the following class hierarchy : 假设我有以下类层次结构:
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;
}
I'd like to databind DataContainer's ContainedData to a ListView and create an ItemTemplate containing : 我想将DataContainer的ContainedData数据绑定到ListView并创建一个包含以下内容的ItemTemplate:
My goals : 我的目标 :
Questions : 问题:
For getting the MyEnum values into an ItemsControl such as a ComboBox, see http://blogs.msdn.com/wpfsdk/archive/2007/02/22/displaying-enum-values-using-data-binding.aspx . 要将MyEnum值转换为ItemsControl(如ComboBox),请参阅http://blogs.msdn.com/wpfsdk/archive/2007/02/22/displaying-enum-values-using-data-binding.aspx 。 To display this in a DataTemplate within a ListView you will use the CellTemplate property: 要在ListView中的DataTemplate中显示它,您将使用CellTemplate属性:
<DataTemplate x:Key="DataEnumTemplate">
<ComboBox ItemsSource="..." SelectedItem="{Binding DataEnum, Mode=TwoWay}" />
</DataTemplate>
<GridViewColumn CellTemplate="{StaticResource DataEnumTemplate" />
(where the ItemsSource is per the linked article). (ItemsSource是根据链接的文章)。
Re data types, a TextBox.Text binding will automatically convert between the text string and the ushort or whatever, and will automatically signal a validation error if the string is not convertible (eg "abc"). 重新数据类型,TextBox.Text绑定将自动在文本字符串和ushort之间进行转换,如果字符串不可转换(例如“abc”),它将自动发出验证错误信号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.