使用依赖项属性和样式触发器时,...不是DependencyProperty [英] `...is not a DependencyProperty` when using dependency properties and style triggers
问题描述
在我的UserControl中:
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()));
然后,当尝试在XAML中使用我的控件并使用datatriggers绑定到 Channel
时:
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>
在XAML中引发以下错误:
Throws the following error(s) in XAML:
渠道"属性不是DependencyProperty.用于标记,非附加属性必须在目标类型上公开具有可访问的实例属性"Channel".对于附件属性,声明类型必须提供静态的"GetChannel"属性.和"SetChannel"方法.
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.
但是与错误让我相信的错误相反(我以某种方式设置了DP错误),以下似乎可以正常工作:
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}">
仅当在DataTrigger中将其用作Setter属性时,DP似乎才出现问题...
Meaning it ONLY seems to have an issue with the DP when it is used in a DataTrigger as a Setter property...
推荐答案
尝试命名静态属性 ChannelProperty
而不是 ChannelDP
.
Try naming your static property ChannelProperty
instead of ChannelDP
.
这篇关于使用依赖项属性和样式触发器时,...不是DependencyProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!