更新时间:2023-10-13 11:13:04
这似乎是使用XAML时,这是不可能的。自定义控件似乎是一个矫枉过正的时候我居然有我需要的所有控制,但只需将它们分组连同逻辑的一个小一点,并允许指定的内容。
It seems this is not possible when XAML is used. Custom controls seem to be a overkill when I actually have all the controls I need, but just need to group them together with a small bit of logic and allow named content.
在 JD的博客作为 mackenir暗示,似乎有***的折衷办法。扩展JD的解决办法,让仍然在XAML中定义的控件可能如下:
The solution on JD's blog as mackenir suggests, seems to have the best compromise. A way to extend JD's solution to allow controls to still be defined in XAML could be as follows:
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
var grid = new Grid();
var content = new ContentPresenter
{
Content = Content
};
var userControl = new UserControlDefinedInXAML();
userControl.aStackPanel.Children.Add(content);
grid.Children.Add(userControl);
Content = grid;
}
在我上面的例子我已经创建了一个名为UserControlDefinedInXAML用户控件是这样定义使用XAML任何普通用户控件。在我的UserControlDefinedInXAML我有一个叫StackPanel的在其中我希望我的命名内容出现aStackPanel。
In my example above I have created a user control called UserControlDefinedInXAML which is define like any normal user controls using XAML. In my UserControlDefinedInXAML I have a StackPanel called aStackPanel within which I want my named content to appear.