動作していない単純な依存関係プロパティがいくつかあります。私はそれらを調べ、過去に使用したコードを調べましたが、なぜそれらが機能しないのかわかりません。
UserControlを拡張するカスタム基本クラス(MyBaseControl)があり、カスタムUIコントロールはそれを拡張します。たとえば、MyCustomControlはMyBaseControlを拡張します。
MyCustomControlXAMLは非常に単純です。
<StackPanel>
<Image Source="{Binding Icon}" />
<TextBlock Text="{Binding Blurb}" />
</StackPanel>
MyCustomControlコードは次のようになります。
public partial class MyCustomControl: MyBaseControl
{
//public static readonly DependencyProperty IconProperty = Image.SourceProperty.AddOwner(typeof(MyCustomControl));
//public static readonly DependencyProperty BlurbProperty = TextBlock.TextProperty.AddOwner(typeof(MyCustomControl));
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register(
"Icon",
typeof(ImageSource),
typeof(MyCustomControl),
new PropertyMetadata(null));
public static readonly DependencyProperty BlurbProperty =
DependencyProperty.Register(
"Blurb",
typeof(String),
typeof(MyCustomControl),
new PropertyMetadata(null));
public MyCustomControl() : base()
{
InitializeComponent();
}
#region Properties
public ImageSource Icon
{
get { return (ImageSource)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
}
public String Blurb
{
get { return (String)GetValue(BlurbProperty); }
set { SetValue(BlurbProperty, value); }
}
#endregion Properties
}
を定義するためにいくつかの異なる方法を試したことに注意してくださいDependencyProperty
。どちらも機能しません。
私は次のように自分のコントロールを呼び出します。
<ctrl:MyCustomControl Height="240" VerticalAlignment="Center" HorizontalAlignment="Center" Width="320" Blurb="Slide Show" Icon="pack://application:,,,/Resources/photo_scenery.png" />
ソースまたはテキストをXAMLで直接設定すると、問題なく表示されます。バインディングは正しく機能することを望んでいません。
バインディングを通過させないのに何が欠けていますか?
助けてくれてありがとう!
更新:コメントと試した追加の変更に基づいてコードを更新しました。