DependencyProperty只是有时绑定,并且在设计器中报告为缺少的属性

本文关键字:报告 属性 绑定 DependencyProperty | 更新日期: 2023-09-27 18:11:40

我有一个非常奇怪的问题与用户控件的依赖属性。

编译,但是文本以预期的方式出现。color属性适用于前两种使用(endColor和progressColor),但不适用于文本的前景色。

即使它应该应用正确的颜色,endColor的值。Color,它只返回黑色——默认值。

虽然它编译并应该工作,但设计人员报告:"它找不到属性并将我的XAML标记为无效"。

我已经重新启动VS并删除了设计器shadowcache,但无济于事。

我在主窗口中这样使用它:

<local:HoverButton x:Name="cancelButton" Text="Cancel" Color="Crimson" />

我的xaml:

<UserControl x:Class="gbtis.HoverButton" x:Name="hoverButton"
             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" 
             xmlns:local="clr-namespace:gbtis"
             mc:Ignorable="d">
    <Grid>
        <Border x:Name="borderBox" BorderThickness="4" CornerRadius="2">
            <Border.BorderBrush>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop x:Name="endColor" Color="{Binding Path=Color, ElementName=hoverButton, UpdateSourceTrigger=PropertyChanged}" Offset="0"/>
                    <GradientStop x:Name="progressColor" Color="{Binding Path=Color, ElementName=hoverButton, UpdateSourceTrigger=PropertyChanged}" Offset="1"/>
                    <GradientStop x:Name="progressBlack" Color="Black" Offset="1"/>
                    <GradientStop Color="Black" Offset="1"/>
                </LinearGradientBrush>
            </Border.BorderBrush>
            <Grid>
                <Viewbox>
                    <TextBlock x:Name="textBox"  Margin="2,0,2,0" 
                               Foreground="{Binding Color, ElementName=hoverButton, UpdateSourceTrigger=PropertyChanged}"
                               Text="{Binding Text, ElementName=hoverButton, UpdateSourceTrigger=PropertyChanged}" />
                </Viewbox>
            </Grid>
        </Border>
    </Grid>
</UserControl>

我的控制代码:

    public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(HoverButton), new FrameworkPropertyMetadata(Colors.Black) { BindsTwoWayByDefault = true });
    public Color Color {
        get { return (Color)this.GetValue(ColorProperty); }
        set { SetValueDp(ColorProperty, value); }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    void SetValueDp(DependencyProperty property, object value,
        [System.Runtime.CompilerServices.CallerMemberName] String p = null) {
        SetValue(property, value);
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(p));
    }

更新:

我删除了我用来尝试强制更新的事件,并给文本框一个solidcolorbrush。现在这些属性似乎可以工作了。

然而

…设计器仍然显示它们为不可识别或不可访问

DependencyProperty只是有时绑定,并且在设计器中报告为缺少的属性

指定的绑定不工作的原因是因为您将类型为BrushTextBlock.Foreground属性绑定到类型为ColorHoverButton.Color属性,并且框架无法将Color转换为Brush

所以为了解决这个问题,你要么需要通过Binding.Converter属性提供一个转换器来完成这项工作,要么提供一个刷子,让它使用HoverButton.Color的值。在本例中,我认为您要寻找的画笔是SolidColorBrush:

<TextBlock (...)>
    <TextBlock.Foreground>
        <SolidColorBrush Color="{Binding Color, ElementName=hoverButton, UpdateSourceTrigger=PropertyChanged}" />
    </TextBlock.Foreground>
</TextBlock>

请注意,如果您使用第二种解决方案,将绑定设置为TwoWay几乎没有意义。

就像clemens说的依赖属性不是这样工作的。你可以使用它的"PropertyChangedCallback"

public Color Color {
    get { return (Color)this.GetValue(ColorProperty); }
    set { SetValue(ColorProperty, value); }
}
public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(HoverButton), new PropertyMetadata(Colors.Black, PropertyChanged));
public static void PropertyChanged(DependencyObject dependency, DependencyPropertyChangedEventArgs e)
{
    //some code
}

但这只是需要的,如果你想在属性改变时做一些事情,它不需要通知GUI。

顺便说一下,你的数据上下文应该是这样的:
public HoverButton()
{
    InitializeComponent();
    (this.Content as FrameworkElement).DataContext = this;
}

这样你的CodeBehind就可以绑定到UserControl, UserControl从它的父类继承DataContext。

编辑:

Grx70是正确的。转换也不能工作 http://blog.scottlogic.com/2010/07/09/a-universal-value-converter-for-wpf.html