[英]Binding to Parent of nested UserControl during design-time and runtime
I am working with a custom window class that adds some behaviours to the window: 我正在使用自定义窗口类,该类向窗口添加一些行为:
class CustomWindow : Window {
public CustomWindow() {
// ...
}
}
My current MainWindow.xaml
looks as expected: 我当前的MainWindow.xaml
看起来像预期的那样:
<local:CustomWindow ...>
<!-- ... -->
</local:CustomWindow>
Now I wanted to host the content of the window in a custom container which should be the root element of the window itself but I struggled to implement the automatic creation of the container in the first place. 现在,我想将窗口的内容托管在一个自定义容器中,该容器应该是窗口本身的根元素,但是我首先要实现容器的自动创建。 Thus I am doing manually what I wanted to automate now with a "hardcoded" control: 因此,我正在手动执行我现在想使用“硬编码”控件进行自动化的操作:
<local:CustomWindow ...>
<local:CustomUserControl>
<!-- ... -->
</local:CustomUserControl>
</local:CustomWindow>
In order to display the content that has been added via XAML I have introduced a Content property like this: 为了显示通过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);
}
}
And a ContentPresenter
in the XAML file with Content="{Binding Path=InnerContent, ElementName=customUserControl}"
. 以及XAML文件中的ContentPresenter
和Content="{Binding Path=InnerContent, ElementName=customUserControl}"
。 At this point I am struggling with some further bindings I want to achieve. 在这一点上,我正在努力实现一些其他的绑定。 I have a TextBlock I want to bind to a property of the parent window now. 我有一个TextBlock,现在我想绑定到父窗口的属性。 The hierarchy looks as follows: 层次结构如下所示:
CustomWindow
|- CustomUserControl
|- ...
|- TextBlock
When I set the Text
property to {Binding Path=Title, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:CustomWindow}}}
this works during runtime of course but not during design-time in the window designer. 当我将Text
属性设置为{Binding Path=Title, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:CustomWindow}}}
时,这当然会在运行时起作用,但在窗口设计器的设计时不会起作用。 And of course I cannot use this binding with a design DataContext
that I have introduced like this d:DataContext="{d:DesignInstance local:FakeCustomUserControlData, IsDesignTimeCreatable=True}"
. 当然,我不能将这种绑定与我已经引入的d:DataContext="{d:DesignInstance local:FakeCustomUserControlData, IsDesignTimeCreatable=True}"
这样的设计DataContext
一起使用。
At this point I have two questions: 在这一点上,我有两个问题:
CustomUserControl.xaml
designer, while in the MainWindow.xaml
designer and during runtime I get the real data about the parent? 是否可以在CustomUserControl.xaml
设计器中拥有设计时数据,而在MainWindow.xaml
设计器中以及运行时,我可以获取有关父级的真实数据? (I noticed during testing that the Parent
property of the CustomUserControl
is null during its constructor so I wasn't able to obtain the data of a parent window if there exists one.) (在测试过程中,我注意到CustomUserControl
的Parent
属性在其构造函数期间为null,因此,如果存在父窗口,则我无法获取它的数据。) CustomUserControl
into the window without manually having to add it to the window? 实际上有没有一种方法可以将CustomUserControl
“注入”到窗口中,而无需手动将其添加到窗口中? I tried to replace the Content
property in the CustomWindow
constructor but it got replaced during some point and was never visible. 我试图替换CustomWindow
构造函数中的Content
属性,但是在某些时候它被替换了并且从不可见。 Is there actually a way to "inject" the
CustomUserControl
into the window without manually having to add it to the window? 实际上有没有一种方法可以将CustomUserControl
“注入”到窗口中,而无需手动将其添加到窗口中?
Define a custom template for the window that includes your UserControl
: 为包含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>
Is it possible to have design-time data in the CustomUserControl.xaml designer, while in the
MainWindow.xaml
designer and during runtime I get the real data about the parent? 是否可以在CustomUserControl.xaml设计器中拥有设计时数据,而在MainWindow.xaml
设计器中以及运行时,我可以获取有关父级的真实数据?
You should be able to set a design time data context for the UserControl
: WPF data context for design time and run time . 您应该能够为UserControl
: WPF数据上下文设置设计时间和运行时间的设计时间数据上下文 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.