WPF ListView ItemTemplate Questions

Let's say that I have 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 would like to bind a DataContainer DataContainer ContainedData to a ListView and create an ItemTemplate containing:

        

My goals:

  • I want Combobox to be able to display all possible MyEnum values
  • I want Combobox to implement TwoWay binding to DataEnum field

Questions:

  • How to achieve these goals?
  • Data properties are of different types. Does it matter for a TextBox? If so, should they only be shown as strings? How to check the data? (i.e. make sure the user does not pass "abc" to the DataId field, etc.).
+3
source share
1 answer

MyEnum ItemsControl, ComboBox, . http://blogs.msdn.com/wpfsdk/archive/2007/02/22/displaying-enum-values-using-data-binding.aspx. DataTemplate ListView, CellTemplate:

<DataTemplate x:Key="DataEnumTemplate">
  <ComboBox ItemsSource="..." SelectedItem="{Binding DataEnum, Mode=TwoWay}" />
</DataTemplate>

<GridViewColumn CellTemplate="{StaticResource DataEnumTemplate" />

( ItemsSource - ).

Re , TextBox.Text ushort - , , (, "abc" ).

+5

Source: https://habr.com/ru/post/1724782/


All Articles