添加属性自定义WPF控件? [英] Adding Properties to Custom WPF Control?

查看:146
本文介绍了添加属性自定义WPF控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始WPF今天上午所以这是(希望)一个简单的问题来解决。我开始创建一个按钮,有一个渐变背景。我要声明的控件的属性渐变开始和结束的颜色,然后应用它们在模板中。我有麻烦了code到,虽然编译。我得到的例外情况是,XAML是告诉我的财产是无法访问的,但是当我昌可视性修改过的公共它抱怨找不到静态属性...

下面是我的XAML至今:

 < StackPanel的>
    < StackPanel.Resources>
        <风格的TargetType =我:GradientButton>
            < setter属性=模板>
                < Setter.Value>
                    <的ControlTemplate的TargetType ={X:键入我:GradientButton}>
                        <电网>
                            <椭圆宽度={TemplateBinding宽度}高度={TemplateBinding身高}行程={TemplateBinding前景}VerticalAlignment =热门的Horizo​​ntalAlignment =左>
                                < Ellipse.Fill>
                                    <一个LinearGradientBrush>
                                        在这条线的问题!!!  -   - ><渐变停止颜色={TemplateBinding GradientStart}偏移=0>< /渐变停止>&LT!;
                                        &所述;渐变停止颜色={TemplateBinding GradientEnd}偏移=1>&所述; /渐变停止>
                                    < /一个LinearGradientBrush>
                                < /Ellipse.Fill>
                            < /椭圆>
                            <多边形点=18,12 18,38,35,25填充={TemplateBinding前景}/>
                        < /网格>
                    < /控件模板>
                < /Setter.Value>
            < /二传手>
        < /样式和GT;
    < /StackPanel.Resources>
    <我:GradientButton X:名称=btnPlay高度=50WIDTH =50前景=黑点击=Button_ClickGradientStart =#CCCCCCGradientEnd =#7777777/>
< / StackPanel的>
 

和这里的code我的自定义控件:

 公共类GradientButton:按钮
{
    静态的DependencyProperty GradientStartProperty;
    静态的DependencyProperty GradientEndProperty;

    静态GradientButton()
    {
        GradientStartProperty = DependencyProperty.Register(GradientStart的typeof(彩色)的typeof(GradientButton));
        GradientEndProperty = DependencyProperty.Register(GradientEnd的typeof(彩色)的typeof(GradientButton));
    }

    众彩GradientStart
    {
        {返回(彩色)base.GetValue(GradientStartProperty); }
        集合{base.SetValue(GradientStartProperty,价值); }
    }

    众彩GradientEnd
    {
        {返回(彩色)base.GetValue(GradientEndProperty); }
        集合{base.SetValue(GradientEndProperty,价值); }
    }
}
 

编辑: 这里的设计时除了我得到

 不能引用静态成员GradientStartProperty的类型GradientButton',因为它是不可访问。
 

解决方案

我想它了... 这:

 静态的DependencyProperty GradientStartProperty;
静态的DependencyProperty GradientEndProperty;
 

需要改变,以这样的:

 公共静态的DependencyProperty GradientStartProperty;
公共静态的DependencyProperty GradientEndProperty;
 

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.

解决方案

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;

这篇关于添加属性自定义WPF控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆