WPF绑定词典<;字符串,列表<;字符串>;要列表视图,列表框如何?
如何将字典绑定到ListView和文本框WPF绑定词典<;字符串,列表<;字符串>;要列表视图,列表框如何?,wpf,binding,dictionary,Wpf,Binding,Dictionary,如何将字典绑定到ListView和文本框 namespace Models { public class DynamicList { #region Constructor public DynamicList() { _propDict = new Dictionary<string, object>(); dynimicListProps = new List<DynimicListProperty>(
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();
}
}
命名空间模型
{
公共类动态学家
{
#区域构造函数
公共动态学家()
{
_propDict=新字典();
dynimicListProps=新列表();
}
#端域构造函数
#区域字段
私人词典;
私人IList dynimicListProps;
#端域字段
#区域属性
公共词典
{
获取{return\u propDict;}
设置{u propDict=value;}
}
公共字符串测试字符串
{
获取{return“你好!很好!”;}
}
#端域属性
#区域方法
公共void CreateProperties(字符串[]arrLine)
{
对于(int i=0;i
您可以为KeyValuePair
编写一个数据模板,并将其放入根ListView的ItemsTemplate中。ListView的ItemsSource将是您的字典。在此数据模板中,您将有另一个itemscontrol(例如另一个ListView)其中,您将itemstemplate设置为绑定到字符串的文本框。或者,您可以将单个TreeView与Hierarchy datatemplate一起用于所有内容。您可以使用模板使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;
var数据=新字典
{
{“1”,新名单{“一”、“二”、“三”、“四”},
{“2”,新名单{“五”、“六”、“七”、“八”}
};
this.listBox.ItemsSource=数据;
你能给我一些代码示例吗?我在WPF方面非常新手,给我的答案添加了一个示例。关于如何修改代码以允许选择字典中的各个值的想法?