クラスの通常のプロパティを設定するために、添付の動作を作成しています。
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で指定された値に設定することです。