【问题标题】:Binding to Parent of nested UserControl during design-time and runtime在设计时和运行时绑定到嵌套 UserControl 的父级
【发布时间】:2017-09-28 16:16:26
【问题描述】:

我正在使用一个自定义窗口类,它向窗口添加了一些行为:

class CustomWindow : Window {
    public CustomWindow() {
        // ...
    }
}

我当前的MainWindow.xaml 看起来和预期的一样:

<local:CustomWindow ...>
    <!-- ... -->
</local:CustomWindow>

现在我想将窗口的内容托管在一个自定义容器中,该容器应该是窗口本身的根元素,但我首先努力实现容器的自动创建。因此,我现在使用“硬编码”控件手动执行我想要自动化的操作:

<local:CustomWindow ...>
    <local:CustomUserControl>
        <!-- ... -->
    </local:CustomUserControl>
</local:CustomWindow>

为了显示通过 XAML 添加的内容,我引入了一个 Content 属性,如下所示:

[ContentProperty("InnerContent")]
public partial class CustomUserControl : UserControl
{
    public static readonly DependencyProperty InnerContentProperty =
        DependencyProperty.Register("InnerContent", typeof(object), 
                                    typeof(CustomUserControl));

    public object InnerContent
    {
        get => GetValue(InnerContentProperty);
        set => SetValue(InnerContentProperty, value);
    }
}

XAML 文件中的ContentPresenterContent="{Binding Path=InnerContent, ElementName=customUserControl}"。在这一点上,我正在努力实现我想要实现的一些进一步的绑定。我现在有一个要绑定到父窗口属性的 TextBlock。层次结构如下:

CustomWindow
|- CustomUserControl
   |- ...
      |- TextBlock

当我将Text 属性设置为{Binding Path=Title, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:CustomWindow}}} 时,这当然在运行时有效,但在窗口设计器的设计时无效。当然,我不能将此绑定与我介绍的 DataContext 这样的设计 d:DataContext="{d:DesignInstance local:FakeCustomUserControlData, IsDesignTimeCreatable=True}" 一起使用。

此时我有两个问题:

  1. 是否有可能在CustomUserControl.xaml 设计器中拥有设计时数据,而在MainWindow.xaml 设计器中以及在运行时我可以获得关于父级的真实数据? (我在测试期间注意到CustomUserControlParent 属性在其构造函数期间为空,因此如果存在父窗口,我无法获取父窗口的数据。)
  2. 实际上有没有一种方法可以将CustomUserControl“注入”到窗口中而无需手动将其添加到窗口中?我尝试替换 CustomWindow 构造函数中的 Content 属性,但它在某些时候被替换并且从未可见。

【问题讨论】:

    标签: c# wpf xaml binding


    【解决方案1】:

    实际上有没有一种方法可以将CustomUserControl“注入”到窗口中而无需手动将其添加到窗口中?

    为包含您的UserControl 的窗口定义一个自定义模板:

    <Window x:Class="WpfApp1.Window1"
            ...>
        <Window.Template>
            <ControlTemplate TargetType="Window">
                <AdornerDecorator>
                    <local:CustomUserControl Background="White">
                        <ContentPresenter />
                    </local:CustomUserControl>
                </AdornerDecorator>
            </ControlTemplate>
        </Window.Template>
        <Grid>
            <TextBlock>the content...</TextBlock>
        </Grid>
    </Window>
    

    是否可以在 CustomUserControl.xaml 设计器中拥有设计时数据,而在 MainWindow.xaml 设计器中以及在运行时我获得有关父级的真实数据?

    您应该能够为UserControlWPF data context for design time and run time 设置设计时数据上下文。

    【讨论】:

    • 自从我偶然发现this article 后,我将您的想法与ControlTemplate 相得益彰。而不是作者在模板中使用的自定义 chrome,我只是将我的 CustomUserControl 并能够在 OnApplyTemplate 覆盖中设置它的 DataContext - 这正是我在谈论“注射”。非常感谢您的建议!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    • 2010-11-29
    相关资源
    最近更新 更多