WPF-binding dictionary <string, List <string> in listView, ListBox how?

How to associate a dictionary with a ListView and a text box?

namespace Models
{
  public class DynamicList
  {
    #region Constructor

    public DynamicList()
    {
        _propDict = new Dictionary<string, object>();
        dynimicListProps = new List<DynimicListProperty>();
    }
    #endregion Constructor

    #region Fields

    private Dictionary<string, object> _propDict;
    private IList<DynimicListProperty> dynimicListProps;
    #endregion Fields

    #region Properties
    public Dictionary<string, object> PropsDict
    {
        get { return _propDict; }
        set { _propDict = value; }
    }
    public string TestString
    {
        get { return "Hello! It works!"; }
    }
    #endregion Properties

    #region Methods
    public void CreateProperties(string[] arrLine)
    {
        for (int i = 0; i < arrLine.Count(); i++)
        {
            _propDict.Add(arrLine[i].Replace("\"",""), null);
        }
    }

    #endregion Methods
  }

  public class DynimicListProperty
  {
    private IList<string> propertyNameValues = new List<string>();

    public IList<string> PropertyNameValues
    {
        get { return propertyNameValues; }
        set { propertyNameValues = value; }
    }
  }
}

// now try to bind

private Models.DynamicList _dynimicList;
public Models.DynamicList _DynimicList
        {
            get { return _dynimicList; }
        }
CreateView()
{
    _importView = new Views.ImportBomView();
                _importView.Grid1.DataContext = _DynimicList;
                Binding bn = new Binding("Value.[2]");
                bn.Mode = BindingMode.OneWay;
                bn.Source = _DynimicList.PropsDict.Keys;
                _importView.tbFileName.SetBinding(TextBlock.TextProperty, bn);
  /////////////////////////////////////////////////////////////////////////////         
 //_importView.listView1.ItemsSource = (IEnumerable)_DynimicList.PropsDict["Value"];
 ////// it works when Binding bn2 = new Binding("") but of course in 
 ///this emplementation I have the same data in all columns - so not good
 /////////////////////////////////////////////////////////////////////////////////////

           // here I'll like to generate Columns and bind gvc.DisplayMemberBinding
           // to dictionary _DynimicList.PropsDict[item] with Key=item
           foreach (var item in _DynimicList.PropsDict.Keys)
            {
                Binding bn2 = new Binding("[3]");
                bn2.Source = (IEnumerable)_DynimicList.PropsDict[item];
                GridViewColumn gvc = new GridViewColumn();
                gvc.DisplayMemberBinding = bn2;
                gvc.Header = item;
                gvc.Width = 100;
                _importView.gridView1.Columns.Add(gvc);
            }
            _importView.Show();
        }

}
+3
source share
1 answer

You can write a datatemplate for KeyValuePair<string, List<string>>and put it in the ItemsTemplate of the root ListView. The ItemsSource element of your ListView will be your dictionary. In this datatemplate, you will have another control (for example, a different list), where you set the itemstemplate in the text box that binds to the string. Alternatively, you can use one TreeView for everything combined with a hierarchical dataset. You can make TreeView, but want to use templates.

<ListBox Name="listBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <ContentPresenter Content="{Binding Key}" Margin="0 0 4 0"/>
                <ItemsControl ItemsSource="{Binding Value}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemContainerStyle>
                        <Style TargetType="ContentPresenter">
                            <Setter Property="Margin" Value="0 0 2 0" />
                        </Style>
                    </ItemsControl.ItemContainerStyle>
                </ItemsControl>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

:

var data = new Dictionary<string, List<string>>
{
    {"1", new List<string> {"one", "two", "three", "four"}},
    {"2", new List<string> {"five", "six", "seven", "eight"}}
};
this.listBox.ItemsSource = data;
+10

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


All Articles