繁体   English   中英

在WPF中重新定义ContentControl.Content

[英]Redefine ContentControl.Content in WPF

我正在尝试在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 我该如何工作?

您可能需要在“您的”类中将一个属性指示为“内容属性”,以便将Dialog的XAML“内容”描述的子元素放入其中,而不是放在基本Window的“ content”属性中。

[ContentProperty("Content")]
public class Dialog : Window {

如果那不起作用,请尝试更改名称.....所以请尝试以下操作:

[ContentProperty("DialogContent")]
public class Dialog : Window {

public new UIElement DialogContent {
      get { return this.m_mainContent.Child; }
      set { this.m_mainContent.Child = value; }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM