如何公开嵌套在UserControl中的控件的DependencyProperty? [英] How to Expose a DependencyProperty of a Control nested in a UserControl?

查看:125
本文介绍了如何公开嵌套在UserControl中的控件的DependencyProperty?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将图像从窗口向下绑定到UserControl'DisplayHandler'内的UserControl'Display'中.显示具有DependancyProperty'DisplayImage'. 这类似于,但是他们的回答并没有解决我的问题.

I'm trying to bind an Image down from a Window into a UserControl 'Display' thats inside a UserControl 'DisplayHandler'. Display has a DependancyProperty 'DisplayImage'. This is Similar to this, but their answers didn't help with my problem.

DisplayHandler还应具有属性"DisplayImage",并将绑定向下传递到Display.但是Visual Studio不允许我两次注册具有相同名称的DependancyProperty.因此,我尝试不进行两次注册,而只是重复使用:

DisplayHandler also should have the Property 'DisplayImage' and pass down the Binding to Display. But Visual Studio doesn't allow me to register a DependancyProperty with the same name twice. So I tried to not register it twice but only to reuse it:

窗口

<my:DisplayHandler DisplayImage=
    "{Binding ElementName=ImageList, Path=SelectedItem.Image}" />

DisplayHandler

xaml

<my:Display x:Name="display1"/>

cs

public static readonly DependencyProperty DisplayImageProperty =
    myHWindowCtrl.DisplayImageProperty.AddOwner(typeof(DisplayHandler));

public HImage DisplayImage {
    get { return (HImage)GetValue(DisplayImageProperty); }
    set { SetValue(DisplayImageProperty, value); }
}
public HImage DisplayImage /*alternative*/ {
    get { return (HImage)display1.GetValue(Display.DisplayImageProperty); }
    set { display1.SetValue(Display.DisplayImageProperty, value); }
}

**这两个属性均无法解决.*

**neither of the 2 properties worked out.*

显示

public HImage DisplayImage {
    get { return (HImage)GetValue(DisplayImageProperty); }
    set { SetValue(DisplayImageProperty, value); }
}
public static readonly DependencyProperty DisplayImageProperty =
    DependencyProperty.Register("DisplayImage", typeof(HImage), typeof(Display));


我一直在想,如果未定义其自身的值,则控件将沿树向上移动并寻找其属性. ->引用

因此它应该以某种方式工作...

So it should work somehow...

我尝试了Templating和A ContentPresenter,因为它适用于ImageList(ImageList也包含Display),但是我无法像ListBoxItem那样绑定它的值.

I made some attempts with Templating and A ContentPresenter because that worked for the ImageList(ImageList also contains the Display), but I couldn't get it to bind the value like for an ListBoxItem.

推荐答案

AddOwner解决方案应该可以正常工作,但是您必须添加

The AddOwner solution should be working, but you have to add a PropertyChangedCallback that updates the embedded control.

public partial class DisplayHandler : UserControl
{
    public static readonly DependencyProperty DisplayImageProperty =
        Display.DisplayImageProperty.AddOwner(typeof(DisplayHandler),
            new FrameworkPropertyMetadata(DisplayImagePropertyChanged));

    public HImage DisplayImage
    {
        get { return (Image)GetValue(DisplayImageProperty); }
        set { SetValue(DisplayImageProperty, value); }
    }

    private static void DisplayImagePropertyChanged(
        DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var dh = obj as DisplayHandler;
        dh.display1.DisplayImage = e.NewValue as HImage;
    }
}

这篇关于如何公开嵌套在UserControl中的控件的DependencyProperty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆