`…is not a DependencyProperty` when using dependency properties and style triggers

不打扰是莪最后的温柔 提交于 2019-12-12 16:49:26

问题


In my 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()));

Then when trying to use my control in XAML and bind to Channel using datatriggers:

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

Throws the following error(s) in XAML:

The property "Channel" is not a DependencyProperty. To be used in markup, non-attached properties must be exposed on the target type with an accessible instance property "Channel". For attached properties, the declaring type must provide static "GetChannel" and "SetChannel" methods.

But contrary to what the error would have me believe (that i set up the DP wrong somehow), the following seems to work fine:

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

Meaning it ONLY seems to have an issue with the DP when it is used in a DataTrigger as a Setter property...


回答1:


Try naming your static property ChannelProperty instead of ChannelDP.



来源:https://stackoverflow.com/questions/35795670/is-not-a-dependencyproperty-when-using-dependency-properties-and-style-trig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!