0
MVVMのAttached Propertyで作業していて、面白い問題が発生しました。MVVM添付プロパティのターゲットと元のコントロール
私は次のようにボタンにWorkTypeと呼ばれる添付プロパティを作っています:
public static DependencyProperty WorkTypeProperty = DependencyProperty.RegisterAttached("WorkType",
typeof(WorkTypeEnum),
typeof(MyControl),
new PropertyMetadata(WorkTypeChanged));
public static void SetWorkType(DependencyObject target, WorkTypeEnum value)
{
target.SetValue(WorkTypeProperty, value);
}
public static WorkTypeEnum GetWorkType(DependencyObject target)
{
return (WorkTypeEnum)target.GetValue(WorkTypeProperty);
}
public static void WorkTypeClick(object sender, MouseButtonEventArgs e)
{
var control = (Control)sender;
WorkTypeEnume workType = (WorkTypeEnum)control.GetValue(WorkTypeProperty);
(Instance of MyControl).DoWork(workType); ??? <--How to know the instance of MyControl?
}
private static void WorkTypeChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
var control = target as Control;
if (control != null)
{
if ((e.NewValue != null) && (e.OldValue == null))
{
control.MouseDown += WorkTypeClick;
}
else if ((e.NewValue == null) && (e.OldValue != null))
{
control.MouseDown -= WorkTypeClick;
}
}
}
私はそれがMyControl.DoWorkのインスタンスを実行するように、私はボタンにWorkTypeをバインドできるか疑問に思って(WorkTypeEnumワークタイプ)?
ボタンにMyControlのインスタンスを割り当てることはできますか?
ありがとうございました!