Frank Mehlhop
Frank Mehlhop

Reputation: 2222

How to derive from MAUI control and set the style?

I need many times in my app a Border always with the same layout.

The Layout is written right now in KeyStyles.xaml

<Style x:Key="StandardBorderStyle" TargetType="Border">
    <Setter Property="StrokeShape">
        <RoundRectangle CornerRadius="{StaticResource StandardBorderCornerRadius}" />
    </Setter>
    <Setter Property="StrokeThickness" Value="{StaticResource StandardBorderStrokeThickness}" />
    <Setter Property="BackgroundColor" Value="{StaticResource PageBackgroundColor}" />
</Style>

My derived class MyBorder.cs looks like that:

namespace Controls.Borders
{
    public class MyBorder : Border { }
}

How can I set the Style on to MyBorder, so that I can user ... all over my app?

Upvotes: 0

Views: 61

Answers (1)

Liyun Zhang - MSFT
Liyun Zhang - MSFT

Reputation: 14509

You can set it by the following code:

namespace Controls.Borders
{
    public class MyBorder : Border 
    {
        public MyBorder()
        {
            this.SetDynamicResource(Border.StyleProperty, "StandardBorderStyle");
        }
    }
}

Upvotes: 1

Related Questions