WPF结合字典<字符串列表<串GT;到ListView控件,列表框怎么样? [英] WPF binding Dictionary<string, List<string> to listView, ListBox how?

查看:128
本文介绍了WPF结合字典<字符串列表<串GT;到ListView控件,列表框怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何绑定字典ListView和文本框?

 命名空间模型
{
  公共类DynamicList
  {
    #区域构造    公共DynamicList()
    {
        _propDict =新词典<字符串对象>();
        dynimicListProps =新的List< D​​ynimicListProperty>();
    }
    #endregion构造    #地区字段    私人字典<字符串对象> _propDict;
    私人的IList< D​​ynimicListProperty> dynimicListProps;
    #endregion场    #区域属性
    公共字典<字符串对象> PropsDict
    {
        {返回_propDict; }
        集合{_propDict =价值; }
    }
    公共字符串的TestString
    {
        {返回你好这是作品! }
    }
    #endregion属性    #区域方法
    公共无效CreateProperties(字符串[] arrLine)
    {
        的for(int i = 0; I< arrLine.Count();我++)
        {
            _propDict.Add(arrLine [I] .Replace(\\,),NULL);
        }
    }    #endregion方法
  }  公共类DynimicListProperty
  {
    私人的IList<串GT; propertyNameValues​​ =新的List<串GT;();    公众的IList<串GT; PropertyNameValues
    {
        {返回propertyNameValues​​; }
        集合{propertyNameValues​​ =价值; }
    }
  }
}//现在尝试绑定私人Models.DynamicList _dynimicList;
公共Models.DynamicList _DynimicList
        {
            {返回_dynimicList; }
        }
CreateView的()
{
    _importView =新Views.ImportBomView();
                _importView.Grid1.DataContext = _DynimicList;
                结合BN =新的绑定(值[2]。);
                bn.Mode = BindingMode.OneWay;
                bn.Source = _DynimicList.PropsDict.Keys;
                _importView.tbFileName.SetBinding(TextBlock.TextProperty,BN);
  ////////////////////////////////////////////////// ///////////////////////////
 //_importView.listView1.ItemsSource =(IEnumerable的)_DynimicList.PropsDict [值];
 //////它绑定时的作品BN2 =新的绑定(),但当然在
 ///此emplementation我对所有列相同的数据 - 这样不好
 ////////////////////////////////////////////////// ///////////////////////////////////           //这里我会喜欢来生成列和绑定gvc.DisplayMemberBinding
           //到字典_DynimicList.PropsDict [项目]用钥匙=项目
           的foreach(在_DynimicList.PropsDict.Keys VAR项)
            {
                结合BN2 =新结合([3]);
                bn2.Source =(IEnumerable的)_DynimicList.PropsDict [项目];
                GridViewColumn GVC =新GridViewColumn();
                gvc.DisplayMemberBinding = BN2;
                gvc.Header =项目;
                gvc.Width = 100;
                _importView.gridView1.Columns.Add(GVC);
            }
            _importView.Show();
        }}


解决方案

您可以写一个DataTemplate KeyValuePair<字符串列表<串>> 并把它放在根的ListView的ItemsTemplate。你的ListView的ItemsSource的将是你的字典。在这个DataTemplate中,你将有另一个ItemsControl的(如列表视图另一个),您的itemstemplate设置绑定到字符串文本框。另外,您可以使用一个单一的TreeView与层次的DataTemplate一切结合。你可以,但是你想使用模板做一个TreeView的样子。

 <列表框名称=列表框>
    < ListBox.ItemTemplate>
        <&DataTemplate的GT;
            < StackPanel的方向=横向>
                <内容presenter CONTENT ={结合重点}保证金=0 0 4 0/>
                <的ItemsControl的ItemsSource ={绑定值}>
                    < ItemsControl.ItemsPanel>
                        < ItemsPanelTemplate>
                            < StackPanel的方向=横向/>
                        < / ItemsPanelTemplate>
                    < /ItemsControl.ItemsPanel>
                    < ItemsControl.ItemContainerStyle>
                        <风格的TargetType =内容presenter>
                            < setter属性=保证金VALUE =0 0 2 0/>
                        < /样式和GT;
                    < /ItemsControl.ItemContainerStyle>
                < / ItemsControl的>
            < / StackPanel的>
        < / DataTemplate中>
    < /ListBox.ItemTemplate>
< /列表框>

和背后的一些样本数据code:

  VAR数据=新的解释和LT;字符串列表<串GT;>
{
    {1,新的List<串GT; {一二三四}},
    {2,新的List<串GT; {十二五,六个一,七,八}}
};
this.listBox.ItemsSource =数据;

how to bind Dictionary to ListView and 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();
        }

}

解决方案

You can write a datatemplate for KeyValuePair<string, List<string>> and put it in the ItemsTemplate of the root ListView. The ItemsSource of your ListView would be your dictionary. In this datatemplate you would have another itemscontrol (such as another listview) where you set the itemstemplate to a textbox that binds to the string. Alternatively you could use a single TreeView for everything in conjunction with hierarchical datatemplate. You can make a TreeView look however you want using 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>

And in the code behind some sample data:

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结合字典&LT;字符串列表&LT;串GT;到ListView控件,列表框怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆