如何从UserControl XAML中正确引用自定义DependencyProperty?

时间:2019-06-06 06:59:04

标签: c# wpf visual-studio xaml

VisualStudio有点奇怪的问题。这可能是IDE的一个错误,但是我想知道我的处理方式是否有任何异常。

首先,我有一个UserControl,它定义了Resources标签。我在该标记中设置了一个样式,该样式将应用于我派生的UserControl(StackEntry)。我正在使用DataTrigger将有条件的自定义控件上的DependencyProperty设置为某个值。

有趣的是,尽管Visual Studio告诉我我有两个错误,但我能够很好地构建解决方案。它甚至可以完全按预期运行-DependencyProperty由触发器设置。但是,我仍然想弄清楚是什么原因造成的,所以我可以重新获得设计窗口。当前正在显示“无效标记”消息。

我尝试过:

  1. 使用不同的属性。自定义属性从不起作用,但UserControl中定义的属性可以起作用。

  2. 在单独的文件(资源)中设置触发器。

  3. 重新启动Visual Studio。这将导致“错误”暂时解决,直到它似乎以任意间隔重新出现。

  4. 重建项目。

  5. 运行项目。

以下是相关的XAML:

<UserControl.Resources>
    <Style TargetType="{x:Type con:StackEntry}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=Control, Path=Element.EntityConditionState.IsEmpty}" Value="True">
                <Setter Property="ConditionRowHeight" Value="0"/> <!-- this is the line throwing the "error". -->
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

我引用的DependencyProperty:

public GridLength ConditionRowHeight
{
    get => (GridLength)GetValue(ConditionRowHeightProperty);
    set => SetValue(ConditionRowHeightProperty, value);
}
public static readonly DependencyProperty ConditionRowHeightProperty =
    DependencyProperty.Register("ConditionRowHeight", typeof(GridLength),
    typeof(StackEntry), new PropertyMetadata(new GridLength(0.8, GridUnitType.Star)));

由于StackElement类定义了ConditionRowHeight DP,因此设置Style标签的TargetType应该允许任何Setter标签使用它。但是,出现以下错误:

属性“ ConditionRowHeight”不是DependencyProperty。要在标记中使用,非附加属性必须使用可访问的实例属性“ ConditionRowHeight”在目标类型上公开。对于附加属性,声明类型必须提供静态的“ GetConditionRowHeight”和“ SetConditionRowHeight”方法。

此外,如上所述,XAML显示窗口向我显示“无效标记”消息。

我正在使用Visual Studio Community 2019,如果有帮助的话。

0 个答案:

没有答案
相关问题