【问题标题】:DependencyProperty not working on custom UserControlDependencyProperty 不适用于自定义 UserControl
【发布时间】:2012-07-19 17:34:32
【问题描述】:

我有一些简单的依赖属性不起作用。我查看了它们,查看了我过去使用过的代码,但我不确定它们为什么不起作用。

我有一个扩展 UserControl 的自定义基类 (MyBaseControl),然后我的自定义 UI 控件在此基础上进行扩展。例如,MyCustomControl 扩展了 MyBaseControl。

MyCustomControl XAML 很简单:

<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 中设置 Source 或 Text,它们会显示得很好。绑定只是不想正常工作。

我错过了什么不允许我的绑定通过?

感谢您的帮助!

更新:我已根据 cmets 和我尝试的其他更改更新了代码。

【问题讨论】:

    标签: wpf user-controls wpf-controls dependency-properties


    【解决方案1】:

    您错误地注册了 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>
    

    【讨论】:

    • 感谢 RV1987。我已经准备好在空中挥舞我的拳头了,但看起来我的代码至少有两个问题。我进行了上述更改,但仍然看不到我的图像。
    • 我已经更新了答案。您需要为绑定指定 relativeSource。
    • 绑定上的 RelativeSource 做到了。谢谢!我将需要再次阅读所有这些内容 - 因为我不了解对它们的需求,但它现在有效!再次感谢!
    【解决方案2】:

    或者,您可以通过更新代码来解决问题,而不是按照 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-20
      • 2018-12-07
      • 2020-06-16
      • 2020-08-09
      • 1970-01-01
      • 2012-06-06
      • 1970-01-01
      • 2013-06-03
      • 2012-03-02
      相关资源
      最近更新 更多