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