そのコントロールの ResourceDictionary 内から WPF コントロールの要素を参照しようとしています。次に例を示します。
<UserControl.Resources>
<ResourceDictionary>
<Behaviors:GridViewInteractionModel x:Key="gridViewInteraction" GridView="{Binding ElementName=myGridView}"/>
</ResourceDictionary>
</UserControl.Resources>
...
<SomeGridView x:Name="myGridView"/>
GridView
オブジェクトの依存関係プロパティの値は、というGridViewInteractionModel
オブジェクトである必要がありSomeGridView
ますmyGridView
。
上記のコードは機能しません。 {Binding ElementName=myGridView}
要素をバインドしません ( SetValue 関数GridViewInteractionModel
が呼び出されることはありません)。
WPF ランタイム エラーは次のとおりです。
Cannot find source for binding with reference 'ElementName=myGridView'.
BindingExpression:(no path); DataItem=null; target element is
'GridViewInteractionModel' (HashCode=15238415); target property is
'GridView' (type 'SomeGridView')
コントロール内の要素を ResourceDictionary 内のリソースのプロパティにバインドする方法を知っている人はいますか?
プロパティ セットを取得するために私が見つけた唯一の方法は、次のようにコード ビハインド コンストラクターで手動で設定することですInitializeComponent()
。
(Resources["gridViewInteraction"] as GridViewInteractionModel).GridView = FindName("myGridView") as SomeGridView;
しかし、それは本当に醜いです (そしてエラーが発生しやすくなります)。
どうも。