1

動作していない単純な依存関係プロパティがいくつかあります。私はそれらを調べ、過去に使用したコードを調べましたが、なぜそれらが機能しないのかわかりません。

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で直接設定すると、問題なく表示されます。バインディングは正しく機能することを望んでいません。

バインディングを通過させないのに何が欠けていますか?

助けてくれてありがとう!

更新:コメントと試した追加の変更に基づいてコードを更新しました。

4

2 に答える 2

4

Icon プロパティを正しく登録していません。その登録方法では、DP 名を指定する必要があります。つまり、" " の代わりに " IconProperty" にする必要がありますIcon-

public static readonly DependencyProperty IconProperty =
        DependencyProperty.Register(
        "Icon",
        typeof(ImageSource),
        typeof(MyCustomControl),
        new PropertyMetadata(null));

また、次のようにバインディングに RelativeSource を設定してみてください -

<StackPanel>
    <Image Source="{Binding Icon, RelativeSource={RelativeSource 
            Mode=FindAncestor, AncestorType={x:Type ctrl:MyCustomControl}}}" />
    <TextBlock Text="{Binding Blurb, RelativeSource={RelativeSource 
            Mode=FindAncestor, AncestorType={x:Type ctrl:MyCustomControl}}}" />
</StackPanel>
于 2012-07-19T18:01:13.700 に答える
0

または、Rohit Vats が提案するように RelativeSource を設定する代わりに、次のようにコードを更新することで問題を解決できる場合があります。

<StackPanel>
    <Image Name="imgImage" Source="{Binding Icon}" />
    <TextBlock Name="txtTextBlock" Text="{Binding Blurb}" />
</StackPanel>

public LabeledTextBox()
{
    InitializeComponent();
    imgImage.DataContext = this;
    txtTextBlock.DataContext = this;
}
于 2017-09-08T22:43:46.780 に答える