2009-10-06 125 views
3

如何將字典綁定到ListView和文本框?WPF綁定Dictionary <string,List <string>要listView,ListBox怎麼樣?

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(); 
     } 

} 

回答

11

你可以寫一個DataTemplate用於KeyValuePair<string, List<string>>並把它放在根目錄的ListView的ItemsTemplate。 ListView的ItemsSource將成爲你的字典。在這個數據模板中,您將擁有另一個項目控件(例如另一個列表視圖),您可以在其中將itemstemplate設置爲綁定到該字符串的文本框。或者,您可以使用單個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; 
+0

請你給我一些代碼的例子 - 我非常新手在WPF – Janus 2009-10-06 16:18:59

+0

增加了一個例子,我的答案。 – bitbonk 2009-10-09 08:15:18

+6

@Janus請將問題標記爲答案。 – Shimmy 2011-10-03 16:56:49

相關問題