I’m trying to create a dialog class in WPF. This class inherits from Window and provides some default buttons and settings.
The implementation basically looks like this:
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) {
...
}
}
}
As you can see, I redefined the Content property.
Now I want to be able to use this dialog class in XAML like this:
<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>
However, this will use Window.Content when setting the dialog’s contents rather than Dialog.Content. How do I make this work?
You might need to indicate a property in “your” class as being the “content property” so that the child elements described by the XAML “content” of your Dialog get put into it instead of in the “content” property of your base Window.
If that doesn’t work, then try changing the name…..so try this: