Change the UserControl property by using XAML

advertisements

I have my UserControl

<UserControl x:Class="CustomCtrl.MyButton">
   <Button x:Name="Btn" />
</UserControl>

And I use my UserControl in a Window

<Window>
    <Grid>
        <MyButton Background="Aqua" />
    </Grid>
</Window>

I want to change the Background property of the Button Btn using the property Background of my UserControl with XAML.

I tried adding the Background property

public class MyButton: UserControl
{
    public new Brush Background
    {
        get
        { return Btn.GetValue(BackgroundProperty) as Brush; }

        set
        { Btn.SetValue(BackgroundProperty, value); }
    }
}

But it has no effect.
Instead if I use the code MyButtonControl.Background = Brushes.Aqua;, it works.
Why? How can I fix this problem?


The UserControl.Background is taken, UserControls are not custom controls and you have no control over how this propery is used. If you want to change the background of just one control you can expose a new dependency property and bind it to the Button.Background.