Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 字典中对象属性的数据绑定_C#_Wpf_Xaml_Data Binding - Fatal编程技术网

C# 字典中对象属性的数据绑定

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

我对XAML中的数据绑定有疑问。我有一本字典,里面有我的东西。现在在视图中,我希望在字典的第一个listbox键中显示(这没有问题),但在嵌套的listbox中显示该对象的值

So-我在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>
我的目标是在该文本框中显示参数大小,因此我尝试了不同的方法,如:

<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>