使用依赖项属性和样式触发器时,`...不是DependencyProperty`

时间:2016-03-04 12:07:37

标签: c# wpf xaml dependency-properties

在我的UserControl中:

    public ODIF.DeviceChannel Channel
    {
        get { return (ODIF.DeviceChannel)GetValue(ChannelDP); }
        set { SetValue(ChannelDP, value); }
    }
    public static readonly DependencyProperty ChannelDP = DependencyProperty.Register("ChannelProperty", typeof(ODIF.DeviceChannel), typeof(ChannelBox), new PropertyMetadata(new ODIF.DeviceChannel()));

然后尝试在XAML中使用我的控件并使用datatriggers绑定到Channel时:

<local:ChannelBox VerticalAlignment="Top" HorizontalAlignment="Left" Width="200">
    <local:ChannelBox.Resources>
        <local:TypeOfConverter x:Key="TypeOfConverter"/>
    </local:ChannelBox.Resources>
    <local:ChannelBox.Style>
        <Style TargetType="{x:Type local:ChannelBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding mappingConnector, Converter={StaticResource TypeOfConverter}}" Value="{x:Type ODIF:MappingConnector}">
                    <Setter Property="Channel" Value="{Binding mappingConnector.plugin}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding mappingConnector, Converter={StaticResource TypeOfConverter}}" Value="{x:Type ODIF:InitialMappingConnector}">
                    <Setter Property="Channel" Value="{Binding mappingConnector.SourceChannel}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </local:ChannelBox.Style>
</local:ChannelBox>

在XAML中引发以下错误:

  

该物业&#34;频道&#34;不是DependencyProperty。用于   必须在目标类型上公开标记,非附加属性   具有可访问的实例属性&#34;频道&#34;。对于附件   属性,声明类型必须提供静态&#34; GetChannel&#34;和   &#34; SetChannel&#34;   方法

但与错误会让我相信(我以某种方式设置DP错误)相反,以下似乎工作正常:

<local:ChannelBox VerticalAlignment="Top" HorizontalAlignment="Left" Width="200" Channel="{Binding mappingConnector.plugin}">

这意味着当DP在DataTrigger中用作Setter属性时,DP似乎只有问题...

1 个答案:

答案 0 :(得分:4)

尝试命名静态属性ChannelProperty而不是ChannelDP