简体   繁体   English

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

[英]databinding of object property in dictionary

I have question about databinding in XAML. 我对XAML中的数据绑定有疑问。 I have dictionary in which are my objects. 我有字典,这是我的对象。 Now in the view I want show in first listbox key of dictionary (with this no problem), but in nested listbox show values of that object. 现在,在视图中,我想在字典的第一个列表框键中显示(没有问题),但是在嵌套列表框中显示该对象的值。

So - my code in 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>

following with code from ViewModel: 以下是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;
}

and code from model: 和模型中的代码:

public Dictionary<string, Dictionary<string, myDrive>> _dict;

public Dictionary<string, Dictionary<string, myDrive>> Dict
{
    get
    {
        return this._dict;
    }
}

and class myDrive is simple: 而myDrive类很简单:

public class myDrive
{
    public string size = "";

    public string getSize()
    {
        return this.size;
    }
}

My goal is show parameter size in that textbox so I tried different ways like: 我的目标是在该文本框中显示参数大小,因此我尝试了不同的方法,例如:

<TextBlock Text="{Binding Path=Value.size}" />
<TextBlock Text="{Binding Path=Value[size]}" />
<TextBlock Text="{Binding Path=Value.getSize}" />

but no luck :(. Only if I put Value like in example, than can see output string : "AppName.myDrive". 但没有运气:(。仅当我像示例中那样放置Value时,才可以看到输出字符串:“ AppName.myDrive”。

Thanks for any help 谢谢你的帮助

First define property for size variable. 首先定义大小变量的属性。 You can only Databind properties not variables. 您只能使用Databind属性,而不能使用变量。

public class myDrive
{
    public string size = "";

    public string Size{get{return size;}}
    public string getSize()
    {
        return this.size;
    }
}

Once this is done, you can bind like below 完成此操作后,您可以像下面这样进行绑定

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM