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

Is there a way to specify a default property to be referenced by the Path with data binding within XAML? I am looking to be able to do something like what CollectionViewSource does when using Binding.

When you bind to a CollectionViewSource within XAML, it is automatically hooking up Path to the View property.

Eg: {Binding Source={StaticResource cvs}} is the same as {Binding Path=View, Source={StaticResource cvs}}

Is it possible to do the same thing in a custom DependencyObject or POCO?


Set your property as the DataContext. Say you have this class:

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

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

You can set it as the DataContext, say in a Window this way:

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

and in the Window you have a label, you just do this:

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

The Label will show "Carlo".

Now, if you only want the Name to be used as binding you can do this in the Window:

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

And this in the Label:

<Label Content="{Binding}" />
链接地址: http://www.djcxy.com/p/44596.html

上一篇: silverlight绑定

下一篇: 如何在Silverlight / WPF中指定要绑定的默认属性?