【发布时间】:2015-04-24 09:03:14
【问题描述】:
我对 XAML 中的数据绑定有疑问。我有字典,里面是我的对象。现在在视图中,我想在字典的第一个列表框键中显示(这没问题),但在嵌套列表框中显示该对象的值。
所以 - 我在 XAML 中的代码:
<ListBox ItemsSource="{Binding Dict}" Margin="0,0,171,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Key}" />
<ListBox ItemsSource="{Binding Path=Value}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Key}" />
<TextBlock Text="{Binding Path=Value}" /> <!-- HERE -->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
跟随来自 ViewModel 的代码:
public MainWindow()
{
InitializeComponent();
t = new TestModel();
t._dict = new Dictionary<string, Dictionary<string, myDrive>>();
t._dict.Add("folder1", new Dictionary<string, myDrive>());
t._dict["folder1"].Add("file1", new myDrive() { size = "71" });
t._dict.Add("folder2", new Dictionary<string, test>());
t._dict["folder2"].Add("file1", new myDrive() { size = "54" });
t._dict["folder2"].Add("file2", new myDrive() { size = "30" });
this.DataContext = t;
}
和来自模型的代码:
public Dictionary<string, Dictionary<string, myDrive>> _dict;
public Dictionary<string, Dictionary<string, myDrive>> Dict
{
get
{
return this._dict;
}
}
myDrive 类很简单:
public class myDrive
{
public string size = "";
public string getSize()
{
return this.size;
}
}
我的目标是在该文本框中显示参数大小,因此我尝试了不同的方法,例如:
<TextBlock Text="{Binding Path=Value.size}" />
<TextBlock Text="{Binding Path=Value[size]}" />
<TextBlock Text="{Binding Path=Value.getSize}" />
但没有运气:(。只有当我像示例中那样输入值时,才能看到输出字符串:“AppName.myDrive”。
感谢您的帮助
【问题讨论】:
标签: c# wpf xaml data-binding