c# - 向自定义 WPF 控件添加属性?

标签 c# .net wpf xaml

我今天早上才开始使用 WPF,所以(希望)这是一个容易解决的问题。我已经开始创建一个具有渐变背景的按钮。我想在控件的属性中声明渐变开始和结束颜色,然后将它们应用到模板中。不过,我在编译代码时遇到了麻烦。我得到的异常是 xaml 告诉我该属性不可访问,但是当我将可见性修饰符更改为 public 时,它提示找不到静态属性...

到目前为止,这是我的 xaml:

<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>

这是我的自定义控件的代码:

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); }
    }
}

编辑: 这是我得到的设计时异常

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

最佳答案

我想通了... 这:

static DependencyProperty GradientStartProperty; 
static DependencyProperty GradientEndProperty;

需要改成这样:

public static DependencyProperty GradientStartProperty; 
public static DependencyProperty GradientEndProperty;

关于c# - 向自定义 WPF 控件添加属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3188782/

相关文章:

c# - 将 QuantLib 转换为 QuantLib-SWIG C#

c# - 如何从 ViewModel 更改 ToggleButton?

c# - Windows 应用商店应用程序可以将语言从 Javascript 切换到 C# 吗?

c# - 如何对 ObservableCollection<KeyValuePair<int, string> 进行排序

c# - 线程安全的异步方法

c# - 为什么在所有初始订阅者断开连接后 RefCount 不工作? (减少)

c# - 具有多个控件的 WPF 数据绑定(bind)

c# - TextBox RaiseEvent KeyDownEvent 不起作用...(附代码)

c# - 异步编程 APM 与 EAP

c# - 如何读取 de Exchange 数据库?