Ivan  Silkin
Ivan Silkin

Reputation: 485

Create a custom <Page> class derived from the WPF control in C#?

I had been using System.Windows.Controls.Page class in my WPF application before. Now there is a need to create my own class "CustomPage", derived from "System.Windows.Controls.Page",

public class CustomPage : System.Windows.Controls.Page
{
    public long? CustomProperty { get; set; }

    public CustomPage()
    {
         //some code
    }
}

so that

public partial class MyPage : Page
{
    public MyPage()
    {
        InitializeComponent();
    }
}

became

public partial class MyPage : CustomPage
{
    public MyPage()
    {
        InitializeComponent();
        CustomProperty = null;
    }
}

But when I rebuild my solution, the auto-generated code refreshes my code, creating the second part of the class based on "System.Windows.Controls.Page" instead of CustomPage

    /// <summary>
    /// MyPage
    /// </summary>
    public partial class MyPage : System.Windows.Controls.Page, System.Windows.Markup.IComponentConnector {
//...

and I get an error like "your partially defined class should refer to the same base class". How to use the derived CustomPage class instead of System.Windows.Controls.Page and avoid the error? If I try to modify my class in my .xaml file and use <CustomPage> instead of <Page>, I got an error "CustomPage class is not supported" in WPF.

[update 1] here is my .xaml file, nothing interesting here, I can't modify my <Page> class by my <CustomPage> class here, "CustomPage is not supported" error.

<Page x:Class="DerivedPage.MyPage"
      ...
      Title="MyPage">

    <Grid>
        
    </Grid>
</Page>

It's worth mentioning that I'm kind of new to WPF, I don't know if it's possible to use my custom class here

Upvotes: 1

Views: 691

Answers (1)

Miamy
Miamy

Reputation: 2299

You should to tell compiler to use your class explicitly.

 <local:MyPage x:Class="DerivedPage.MyPage"
               xmlns:local="clr-namespace:AppName.DerivedPage"
               ...

Upvotes: 2

Related Questions