【问题标题】:WPF: designer won't set custom propertyWPF:设计器不会设置自定义属性
【发布时间】:2017-10-01 11:11:44
【问题描述】:

在我的 WPF 应用程序中,我通过子类化 TextBox 创建了一个自定义控件。然后我添加了一个名为 BorderWhenRequired 的 DependencyProperty(当 TextBox 表示必填字段时使用的边框),如下所示:

public class TextBoxEx : TextBox
{
    public static readonly DependencyProperty BorderWhenRequiredProperty = DependencyProperty.Register(
        "BorderWhenRequired", typeof(Brush), typeof(TextBoxEx),
        new FrameworkPropertyMetadata(default(Brush), FrameworkPropertyMetadataOptions.AffectsRender)
    );

    public Brush BorderWhenRequired
    {
        get { return (Brush)GetValue(BorderWhenRequiredProperty); }
        set { SetValue(BorderWhenRequiredProperty, value); }
    }
}

然后我创建了一个资源来保存边框的画笔值和控件的样式:

<SolidColorBrush x:Key="RequiredControlBorderBrush">Purple</SolidColorBrush>

<Style TargetType="{x:Type implementations:TextBoxEx}">
    <Setter Property="BorderWhenRequired" Value="{StaticResource RequiredControlBorderBrush}" />
</Style>

但设计师正在向我展示:

ArgumentException: '#FF800080' is not a valid value for the 'MyNs.Common.Controls.Implementations.TextBoxEx.BorderWhenRequired' property on a Setter.

我尝试将资源 RequiredControlBorderBrush 定义为 Brush、Color 和 SolidColorBrush,但我一直收到同样的错误。

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    解决了我自己的问题。万一有人遇到这个:我犯了一个粗心的错误。我的自定义类中的所有颜色属性都定义为 System.Drawing.Brush,而它们本来应该是 System.Window.Media.Brush。

    【讨论】:

    • 我会的,但根据 StackOverflow 的规则,我只能在明天做。
    猜你喜欢
    • 2018-01-27
    • 2013-04-13
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 2013-01-25
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多