我正在尝试在 WPF 中创建一个对话框类。此类继承Window
并提供一些默认按钮和设置。
实现基本上是这样的:
namespace Commons {
public class Dialog : Window {
public new UIElement Content {
get { return this.m_mainContent.Child; }
set { this.m_mainContent.Child = value; }
}
// The dialog's content goes into this element.
private readonly Decorator m_mainContent = new Decorator();
// Some other controls beside "m_mainContent".
private readonly StackPanel m_buttonPanel = new StackPanel();
public Dialog() {
DockPanel content = new DockPanel();
DockPanel.SetDock(this.m_buttonPanel, Dock.Bottom);
content.Children.Add(this.m_buttonPanel);
content.Children.Add(this.m_mainContent);
base.Content = content;
}
public void AddButton(Button button) {
...
}
}
}
如您所见,我重新定义了Content
属性。
现在我希望能够像这样在 XAML 中使用这个对话框类:
<my:Dialog x:Class="MyDialogTest.TestDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:Commons;assembly=Commons"
Title="Outline" Height="800" Width="800">
<!-- Dialog contents here -->
</my:Dialog>
但是,这将Window.Content
在设置对话框的内容时使用,而不是Dialog.Content
. 我该如何进行这项工作?