tags:

views:

20

answers:

1

Hey all,

I just started WPF this morning so this is (hopefully) an easy question to solve. I've started with creating a button that has a gradient background. I want to declare the gradient start and end colors in the property of the control and then apply them in the template. I'm having trouble getting the code to compile though. The exception I'm getting is that the xaml is telling me the property is not accessible but when I chang the visiblity modifier over to public it complains it can't find the static property...

Here's my xaml so far:

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="my:GradientButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type my:GradientButton}">
                        <Grid>
                            <Ellipse Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Stroke="{TemplateBinding Foreground}" VerticalAlignment="Top" HorizontalAlignment="Left">
                                <Ellipse.Fill>
                                    <LinearGradientBrush>
                                        <GradientStop Color="{TemplateBinding GradientStart}" Offset="0"></GradientStop><!--Problem on this line!!!-->
                                        <GradientStop Color="{TemplateBinding GradientEnd}" Offset="1"></GradientStop>
                                    </LinearGradientBrush>
                                </Ellipse.Fill>
                            </Ellipse>
                            <Polygon Points="18,12 18,38, 35,25" Fill="{TemplateBinding Foreground}" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </StackPanel.Resources>
    <my:GradientButton x:Name="btnPlay" Height="50" Width="50" Foreground="Black" Click="Button_Click" GradientStart="#CCCCCC" GradientEnd="#7777777" />
</StackPanel>

And here's the code for my custom control:

public class GradientButton : Button
{
    static DependencyProperty GradientStartProperty;
    static DependencyProperty GradientEndProperty;

    static GradientButton()
    {
        GradientStartProperty = DependencyProperty.Register("GradientStart", typeof(Color), typeof(GradientButton));
        GradientEndProperty = DependencyProperty.Register("GradientEnd", typeof(Color), typeof(GradientButton));
    }

    public Color GradientStart
    {
        get { return (Color)base.GetValue(GradientStartProperty); }
        set { base.SetValue(GradientStartProperty, value); }
    }

    public Color GradientEnd
    {
        get { return (Color)base.GetValue(GradientEndProperty); }
        set { base.SetValue(GradientEndProperty, value); }
    }
}

EDIT: Here's the design-time exception I'm getting

Cannot reference the static member 'GradientStartProperty' on the type 'GradientButton' as it is not accessible.
A: 

I figured it out... This:

static DependencyProperty GradientStartProperty; 
static DependencyProperty GradientEndProperty;

Needed to be changed to this:

public static DependencyProperty GradientStartProperty; 
public static DependencyProperty GradientEndProperty;
Sonny Boy