WPF如何将Dictionary<string, List<string>>绑定到listView和ListBox?

3
如何将字典绑定到ListView和文本框?
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's 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's 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();
        }

}
1个回答

10

你可以为 KeyValuePair<string, List<string>> 编写一个数据模板,并将其放置在根ListView的ItemsTemplate中。你的ListView的ItemsSource将是你的字典。在这个数据模板中,你可以再放置一个itemscontrol(如另一个listview),其中将项模板设置为绑定到字符串的文本框。或者,你可以与分层数据模板一起使用单个TreeView来处理所有内容。你可以使用模板使TreeView看起来任何你想要的样子。

<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;

请问您能否给我一些代码示例 - 我在WPF方面非常新手。 - Janus
在我的回答中添加了一个示例。 - bitbonk
1
有什么想法可以修改代码,以允许选择字典中的个别值? - BenKoshy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接