简体   繁体   English

WPF ListView ItemTemplate问题

[英]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 : 我的目标 :

  1. I want the Combobox to be able to display all possible MyEnum values 我希望Combobox能够显示所有可能的MyEnum值
  2. I want the Combobox to implement a TwoWay binding to the DataEnum field 我希望Combobox实现对DataEnum字段的TwoWay绑定

Questions : 问题:

  1. How do I achieve the goals listed ? 我如何实现列出的目标?
  2. Data's properties are of varying types. 数据的属性有不同的类型。 Does that matter for the TextBox? 对TextBox有用吗? If so, should I expose them as strings only? 如果是这样,我应该只将它们暴露为字符串吗? How do I validate the data ? 如何验证数据? (ie make sure that a user doesn't pass "abc" in the DataId field etc. ) (即确保用户未在DataId字段中传递“abc”等)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM