【问题标题】:How do I complete a DependencyProperty of type "Windows" in Xaml如何在 Xaml 中完成“Windows”类型的 DependencyProperty
【发布时间】:2021-10-11 08:04:36
【问题描述】:

如何在 Xaml 中完成“Windows”类型的 DependencyProperty?

我有:

public Window WindowHandle
        {
            get { return (Window)GetValue(WindowHandleProperty); }
            set { SetValue(WindowHandleProperty, value); }
        }

        public static readonly DependencyProperty WindowHandleProperty =
            DependencyProperty.Register("WindowHandle", typeof(Window), typeof(CustomBox), new PropertyMetadata(null));

应该在“WindowHandle”属性中输入什么来指示这个窗口?

customBox.WindowHandle = this;//in C#
<UControl:CustomBox x:Name="customBox" WindowHandle="?"/><!--in xaml-->

【问题讨论】:

  • WindowHandle="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"?
  • 它有效,非常感谢。

标签: c# xaml custom-controls dependency-properties


【解决方案1】:

要绑定到窗口,你可以使用FindAncestor like :

<UControl:CustomBox ...
  WindowHandle="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 2023-03-23
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    相关资源
    最近更新 更多