繁体   English   中英

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

[英]databinding of object property in dictionary

我对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}" />

但没有运气:(。仅当我像示例中那样放置Value时,才可以看到输出字符串:“ AppName.myDrive”。

谢谢你的帮助

首先定义大小变量的属性。 您只能使用Databind属性,而不能使用变量。

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>

暂无
暂无

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

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