簡體   English   中英

如何在Silverlight / WPF中指定要綁定的默認屬性?

[英]How can I specify a default property to bind to in Silverlight / WPF?

有沒有一種方法可以指定XAML中具有數據綁定的Path引用的默認屬性? 我希望能夠像使用Binding一樣執行CollectionViewSource的操作。

當您綁定到XAML中的CollectionViewSource時,它會自動將Path鏈接到View屬性。

例如:{Binding Source = {StaticResource cvs}}與{Binding Path = View,Source = {StaticResource cvs}}相同

是否可以在自定義DependencyObject或POCO中做同樣的事情?

將您的屬性設置為DataContext。 假設您有這個課程:

public class Person
{
   public string Name { get; set; }

   public Person(string name)
   {
      this.Name = name;
   }
}

您可以將其設置為DataContext,例如在Window中這樣說:

this.DataContext = new Person("Carlo");

在窗口中有一個標簽,只需執行以下操作:

<Label Content="{Binding Name}" />

標簽將顯示“ Carlo”。

現在,如果僅希望將名稱用作綁定,則可以在窗口中執行以下操作:

Person p = new Person("Carlo");
this.DataContext = p.Name;

在標簽中:

<Label Content="{Binding}" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM