2

I have a UserControl RoundButton.xaml :

<UserControl x:Class="app.controls.RoundButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d">
    <Grid>
        <Border x:Name="_Border" Background="Cyan" CornerRadius="2" >
            <Border.Effect>
                <DropShadowEffect x:Name="_Shadow" BlurRadius="0" ShadowDepth="1" Direction="270" Color="White"/>
            </Border.Effect>
           <Button x:Name="_Button" Content="RoundButton" Background="{x:Null}" BorderThickness="0" Padding="10,0,10,0"/>
        </Border>     
    </Grid>
</UserControl>

RoundButton.xaml.cs :

namespace app.controls
{
    public partial class RoundButton : UserControl
    {
        public RoundButton() { InitializeComponent(); }

        [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public new Brush Background
        {
            get { return _Border.Background; }
            set { _Border.Background = value; }
        }
    }
}

And I want to be able to change the Background property in Blend.

When I define the Background property as new, changing it in Blend does not affect my button. When I define it as override i get the error : Error 1 'app.controls.RoundButton.Background.get': cannot override inherited member 'System.Windows.Controls.Control.Background.get' because it is not marked virtual, abstract, or override

What is the best way to override UserControl properties in order to be able to modify them in Blend ?

EDIT:
A workaround that I found is to rename all RoundButton properties and block the UserControl properties form being displayed in Blend :

public new Brush _Background
{
    get { return _Border.Background; }
    set { _Border.Background = value; }
}
[Browsable(false)]
public new Brush Background { get; set; }
Ionut
  • 1,729
  • 4
  • 23
  • 50
  • Does Blend support custom dependency properties (it must)? Then don't make `new Background`, but make a simple `SomeBackground` property. – Sinatr Jul 13 '15 at 13:39
  • Yes, if I rename the property it works fine in Blend, I just want to reuse the property Background in order to avoid the confusion – Ionut Jul 13 '15 at 13:42
  • You are mixing up two different things: normal properties (as you are trying to work with `UserControl.Background`) and *dependency* property (what `UserControl.Background` is). Won't work. You can't expose some property of `_border` to be visible without making it dependency property of your user control and you surely should override default `UserControl.Background` like this (there is **still** [`Control.Background`](https://msdn.microsoft.com/en-us/library/system.windows.controls.control.background.aspx) dependency property). – Sinatr Jul 13 '15 at 13:52
  • I think I don't understand how this properties are working. For example if I rename the `Background` to `_Background` I can see it in Bend, modify it and the Button will change color. I tried adding a `DepaendencyProperty public static readonly DependencyProperty BackgroundProperty = DependencyProperty.Register("Background", typeof(Brush), typeof(RoundButton));` but it's not linked to _Border, so changing it in Blend has no effect on the button. Can you, please, point me to an similar example ? – Ionut Jul 13 '15 at 17:35
  • 1
    You can try to [override](http://stackoverflow.com/a/5653472/1997232) existing dependency property and set [own event handler](http://stackoverflow.com/a/8830205/1997232). – Sinatr Jul 14 '15 at 07:10

0 Answers0