Is it possible to inherit XAML?

I have usercontrols that inherit from a base class that looks like this:

BasePage.cs:

using System.Windows.Controls;

namespace TestPageManager23434
{
    public class BasePage : UserControl
    {
        public string ButtonPreviousText { get; set; }
        public string ButtonNextText { get; set; }

        protected PageManager pageManager;

        public BasePage(PageManager pageManager)
        {
            this.pageManager = pageManager;
        }
    }
}

      

And custom controls that look like this:

XAML:

<local:BasePage x:Class="TestPageManager23434.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:local="clr-namespace:TestPageManager23434"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel Background="White" 
             Width="400"
             Height="400"                
                >
        <TextBlock Text="this is page1"/>
        <TextBlock Text="{Binding Message}"/>
        <Button Content="go to page 2" Click="Button_Click"
                HorizontalAlignment="Left"
                Width="200"
                Height="30"
                />
    </StackPanel>
</local:BasePage>

      

Code for:

using System.Windows.Controls;

namespace TestPageManager23434
{
    public partial class Page1 : BasePage
    {
        public string Message { get; set; }

        public Page1(PageManager pageManager) : base(pageManager)
        {
            InitializeComponent();
            DataContext = this;
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            pageManager.SwitchPage("page2");
        }
    }
}

      

But now, in my XAML parts of my various UserControls, I also have XAML blocks that I would also like to inherit from the base class so that I don't have this block copied in every XAML file:

<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Left">
    <Button 
        Content="{Binding ButtonPreviousText}" 
        Width="150"
        Margin="5"
        Click="ButtonPrevious_Click"/>
    <Button 
        Content="{Binding ButtonNextText}" 
        Width="150"
        Click="ButtonNext_Click" Margin="5"/>
</StackPanel>

      

But since my base class doesn't have XAML, I see two ways:

  • create them in code in the base class
  • create usercontrols for them

Or is there another way to have this XAML in one place, or inherit XAML in some way?

+2


source to share


4 answers


I suggest reading WPF's Overview of Copyright Management on MSDN. I don't think you can achieve what you are trying to do with UserControls. UserControls are pretty much designed to glue a specific set of controls together so that they can be used consistently within the same application. They are really not meant to be legacy.



One thing you can do is use them in the "MasterPage" style (if you are familiar with ASP.NET). You can put ContentControls inside a "base" UserControl and expose various properties that allow other users to inject content into the base control's standard layout. So your other UserControl puts a "base" UserControl as the root element within itself and sets various content properties for whatever elements they want.

+4


source


In your example, your best bet is to create a control called "NavigationControl" and then use it in each of your controls, rather than copying the same block. Without a little context and specificity, it is difficult to give a good solution.



+1


source


Well, you can write xaml and then use XamlReader to parse xaml for objects, this gives you the benefits of writing xml instead of code.

0


source


If your every control has such common elements, you can make a custom control over the regular elements and use it inside every other custom control.

Secondly, I can see that you have business logic in a common element, however, the way of writing event business logic is a little older, you should consider creating commands and implementing commands. It takes a little more research, but it will allow you to design large projects easily.

0


source







All Articles