C# 字典中对象属性的数据绑定
我对XAML中的数据绑定有疑问。我有一本字典,里面有我的东西。现在在视图中,我希望在字典的第一个listbox键中显示(这没有问题),但在嵌套的listbox中显示该对象的值 So-我在XAML中的代码:C# 字典中对象属性的数据绑定,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我对XAML中的数据绑定有疑问。我有一本字典,里面有我的东西。现在在视图中,我希望在字典的第一个listbox键中显示(这没有问题),但在嵌套的listbox中显示该对象的值 So-我在XAML中的代码: <ListBox ItemsSource="{Binding Dict}" Margin="0,0,171,0"> <ListBox.ItemTemplate> <DataTemplate> <Stac
<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>
我的目标是在该文本框中显示参数大小,因此我尝试了不同的方法,如:
<TextBlock Text="{Binding Path=Value.size}" />
<TextBlock Text="{Binding Path=Value[size]}" />
<TextBlock Text="{Binding Path=Value.getSize}" />
但运气不好:(。只有像示例中那样输入值,才能看到输出字符串:“AppName.myDrive”
感谢您的帮助首先定义大小变量的属性。您只能对属性进行数据绑定,不能对变量进行数据绑定
public class myDrive
{
public string size = "";
public string Size{get{return size;}}
public string getSize()
{
return this.size;
}
}
完成后,您可以像下面这样绑定
<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.Size}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
是的,现在可以使用了:)谢谢
<TextBlock Text="{Binding Path=Value.size}" />
<TextBlock Text="{Binding Path=Value[size]}" />
<TextBlock Text="{Binding Path=Value.getSize}" />
public class myDrive
{
public string size = "";
public string Size{get{return size;}}
public string getSize()
{
return this.size;
}
}
<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.Size}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>