Search code examples
c#xamluwpuwp-xaml

How to extend a XAML control with new properties properly?


I have a new philosophic question, which aims at underline differences between old WPF patterns and the new UWP ones.

I would like to extend a standard control (e.g. a Button) with new properties, in UWP environment, not WPF.

In WPF, I noticed that it was possible to create a Custom Control with two files: a Themes/Generics.xaml and a MyCustomControl.cs.

In UWP, it creates only the .cs file... does it mean that if I want to modify even the XAML content of a Button (let's say that I would like to create an IconButton: A Button whose content is a SymbolIcon and a Textblock), it would be impossible?

I know that I could use a "User Control", but it is a generic control and I would loose all the properties of the control that I want to customize (e.g. in my case, the Click event, etc.)... I could even extend a User Control to include all the properties of the wanted control... but it would take too long.

To the point: what is the best and proper way to customize and extend a standard XAML control by code AND Xaml, in UWP environment?

As an example, think at a classic task: creating an IconButton, as I said above... a Button with it's Content beeing a Grid with a SymbolIcon and a Textblock as children.

Thank you.


Solution

  • what is the best and proper way to customize and extend a standard XAML control by code AND Xaml, in UWP environment?

    Simplely answer: You can use templated Control template.

    There is a video about this, you can start from 30mins from this video.

    Generally speaking, there are two kind of ways for us to customize a control:

    1. UserControl
    2. Templated Control

    For UserControl, it is used for light customization and you just need to use it in your own project. For Templated Control, it's a basic way for us to customize our controls. For your scenario, actually it won't have too many differences here, You can just inherit the control you want and then customize it with your code from code level and also Xaml level.