これは、DependencyPropertyを使用して実行できます。
例:あなたのXAMLで
class MyCoolButton : Button
{
public Func<int, int> MyFunc
{
get { return (Func<int, int>)GetValue(MyFuncProperty); }
set { SetValue(MyFuncProperty, value); }
}
// Using a DependencyProperty as the backing store for MyFunc. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyFuncProperty =
DependencyProperty.Register("MyFunc", typeof(Func<int, int>), typeof(MyCoolButton), new PropertyMetadata(null));
}
あなたがそれを設定します:
<controls:MyCoolButton MyFunc="DoSomething" />
そして背後にあるコードで: あなたのクールなボタンから関数を呼び出す
private int DoSomething(int foo)
{
return foo++;
}
同じように簡単です
if (MyFunc != null)
{
MyFunc(value);
}
私は10年前からC#をやっていました。 「要素に追加するFunc」が何であるかを理解するのに役立つことができますか?どのように追加していますか? –
Func型のプロパティを含むクラス(実際にはどのタイプのデリゲートでもかまいません)。要するに、私は戻り値の型を持つハンドラを目指しています。 –
デリゲートを直接XAML属性にバインドすることはできません。メソッド(イベントハンドラと同じ)をラップするか、TypeConverterで '{Binding} 'を使用して' Func'を ' MethodInfo'(IIRC - 実際にはうまくいくかどうかはわかりませんが)。理想的には 'Click 'の代わりに' Command = "{Binding}"を使うべきですが、これは別の議論です)。 – Dai