如何公开嵌套在 UserControl 中的控件的 DependencyProperty? [英] How to Expose a DependencyProperty of a Control nested in a UserControl?
问题描述
我正在尝试将图像从 Window 绑定到 UserControl 'DisplayHandler' 内的 UserControl 'Display'.Display 有一个 DependancyProperty 'DisplayImage'.这类似于this,但他们的回答对我的问题没有帮助.
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
xml
<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...
我对模板和 ContentPresenter 进行了一些尝试,因为它适用于 ImageList(ImageList 也包含显示),但我无法让它像 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
解决方案应该有效,但您必须添加一个 PropertyChangedCallback 更新嵌入式控件.
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屋!