【发布时间】:2010-02-07 10:27:32
【问题描述】:
我正在创建附加行为以设置类的常规属性:
public class LookupHelper
{
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.RegisterAttached("ItemsSource", typeof(object), typeof(LookupHelper), new UIPropertyMetadata(null, OnItemsSourceChanged));
private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as MyControl;
if(control == null)
return;
control.ItemsSource = (IEnumerable)e.NewValue;
}
public static object GetItemsSource(GridColumn column)
{
return column.GetValue(ItemsSourceProperty);
}
public static void SetItemsSource(GridColumn column, object value)
{
column.SetValue(ItemsSourceProperty, value);
}
}
这里,MyControl 上的 ItemsSource 属性是一个常规属性,所以我无法在 Xaml 中绑定它,因此会出现这种附加行为。
现在,当我使用字符串或对象使用此附加属性时,它可以工作并且我设置的断点被命中,但是当我使用绑定标记设置它时,它永远不会运行。为什么这不起作用?
<MyControl ctrl:LookupHelper.ItemsSource="DataSource"/>; //It works
<MyControl ctrl:LookupHelper.ItemsSource="{Binding Path=MyDataSource}"/>; //Does not work
我需要做的是将 ItemsSource 属性设置为 Binding 指定的值。
【问题讨论】:
-
你在哪里设置断点?您认为应该调用的方法/行没有被调用?
-
您是否尝试过使用
Binding的Source属性而不是Path? (您需要将来源指定为StaticResource)
标签: wpf binding attached-properties attachedbehaviors