いくつかのボタンを持つ GridView があります。それらの 1 つは、次のテンプレートによって定義されます。
<DataTemplate x:Name="SubjectItemTemplate">
<Canvas Width="340" Height="170" VerticalAlignment="Top">
<Controls:ThreeImageButton HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,0,0,0"
NormalStateImageSource="{Binding NormalImage}"
HoverStateImageSource="{Binding HoverImage}"
PressedStateImageSource="{Binding PressedImage}" Command="{Binding Path=NavigateToUnitsPage}"
CommandParameter="{Binding}" Canvas.Left="0" Canvas.Top="0">
</Controls:ThreeImageButton>
</Canvas>
</DataTemplate>
ご覧のとおり、ThreeImageButton というカスタム コントロールができました。ボタンは単体で使うと問題なく動きます。しかし、それを DataTemplate に入れると、プロパティがコード ビハインドにバインドされません。
今、私は持っています
x:Name="MyThreeImageButton"
カスタム ボタン定義で。そして、次のようにコード ビハインドに接続します。
<TextBlock Text="{Binding ElementName=MyThreeImageButton, Path=NormalStateImageSource}"/>
(これはテキストを表示するための単なるテストです。実際のコードでは、要素によって参照される別のプロパティに画像ソースを割り当てます)。
現在、TextBlock には何も表示されていません。プロパティに到達するために使用する正しいバインディング構文は何ですか?
ありがとう!
編集: InitializeComponent 関数で変数を設定しており、DependencyProperty で SetValue を使用しています。
編集:より明確にするために、次の情報を追加させてください
シナリオ I: GridView の DataTemplate で:
<UserControl CustomParameter="Literal Text">
ユーザーコントロールで:
<TextBlock Text="{Binding CustomParameter}">
UserControl .cs: this.DataContext = これは機能します!
シナリオ II: GridView の DataTemplate で:
<UserControl CustomParameter="{Binding ValueFromDataItem">
ユーザーコントロールで:
<TextBlock Text="{Binding CustomParameter}">
UserControl .cs: this.DataContext = this nope!