【问题标题】:WPF binding Dictionary<string, List<string> to listView, ListBox how?WPF 绑定 Dictionary<string, List<string> 到 listView, ListBox 怎么样?
【发布时间】:2009-10-06 12:37:06
【问题描述】:

如何将 Dictionary 绑定到 ListView 和 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'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();
        }

}

【问题讨论】:

    标签: wpf binding dictionary


    【解决方案1】:

    你可以为KeyValuePair&lt;string, List&lt;string&gt;&gt;写一个数据模板,放在根ListView的ItemsTemplate中。您的 ListView 的 ItemsSource 将是您的字典。在此数据模板中,您将有另一个项目控件(例如另一个列表视图),您可以在其中将项目模板设置为绑定到字符串的文本框。或者,您可以将单个 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 的新手
    • 在我的回答中添加了一个例子。
    • 关于如何修改代码以允许选择字典中的各个值的想法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 2018-08-04
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    相关资源
    最近更新 更多