locked
Is there any way to build a custom parent control?

    Question

  • Just like TextBox, Image and some other elements inherit from FrameworkElement, i want my UserControl not inherit from UserControl but from a custom partent control. So when i want to check if it's kind of that parent control type, I can just cast and check if it's null.

    It may like this:

    namespace ComponentsUI
    {
        [Windows.Foundation.Metadata.WebHostHidden]
        public class ComponentUI : UserControl
        {
            public ComponentUI()
            { }
    
            public UIElement Content { get; set; }
            public static DependencyProperty ContentProperty { get; }
        }
    
        public class ImageComponentUI: ComponentUI
        {
        }
    
    }

    When i when to check if an element is a component type, just do like this:

    foreach FrameworkElement elem in selectedItems
    {
        ComponentUI comp = elem as ComponentUI
        if(comp != null)
        {
            // Do something here
        }
    

    Is there any way to achieve that?

    Friday, October 10, 2014 7:32 AM

Answers

  • Yes, you can inherit from other controls as well, though it might not make sense for every control.

    If you for example create a new UserControl and change the XAML to look like this:

    <TextBox
        x:Class="MyApp.TestControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:MyApp"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400">
        
    </TextBox>

    and the code behind to look similar to this:

    public sealed partial class TestControl : TextBox
    {
        public TestControl()
        {
            this.InitializeComponent();
        }
    }

    You have created a new Control that is not inheriting from UserControl but from TextBox instead. It compiles fine and acts just like a normal TextBox in the quick test run I've done right now.

    • Proposed as answer by Oliver Ulm Friday, October 10, 2014 9:52 AM
    • Marked as answer by Jeromeee Friday, October 10, 2014 1:04 PM
    Friday, October 10, 2014 9:52 AM

All replies

  • Yes, you can inherit from other controls as well, though it might not make sense for every control.

    If you for example create a new UserControl and change the XAML to look like this:

    <TextBox
        x:Class="MyApp.TestControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:MyApp"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400">
        
    </TextBox>

    and the code behind to look similar to this:

    public sealed partial class TestControl : TextBox
    {
        public TestControl()
        {
            this.InitializeComponent();
        }
    }

    You have created a new Control that is not inheriting from UserControl but from TextBox instead. It compiles fine and acts just like a normal TextBox in the quick test run I've done right now.

    • Proposed as answer by Oliver Ulm Friday, October 10, 2014 9:52 AM
    • Marked as answer by Jeromeee Friday, October 10, 2014 1:04 PM
    Friday, October 10, 2014 9:52 AM
  • Yes, you can inherit from other controls as well, though it might not make sense for every control.

    If you for example create a new UserControl and change the XAML to look like this:

    <TextBox
        x:Class="MyApp.TestControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:MyApp"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400">
        
    </TextBox>

    and the code behind to look similar to this:

    public sealed partial class TestControl : TextBox
    {
        public TestControl()
        {
            this.InitializeComponent();
        }
    }

    You have created a new Control that is not inheriting from UserControl but from TextBox instead. It compiles fine and acts just like a normal TextBox in the quick test run I've done right now.

    Thanks very much.

    I have already figured out that, but still thank you for your reply. I will pay more work before I file any question.


    Friday, October 10, 2014 1:04 PM