`...在使用依赖属性和样式触发器时不是 DependencyProperty`
`...is not a DependencyProperty` when using dependency properties and style triggers
在我的用户控件中:
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 中使用我的控件并使用数据触发器绑定到 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 中引发以下错误:
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),以下似乎工作正常:
<local:ChannelBox VerticalAlignment="Top" HorizontalAlignment="Left" Width="200" Channel="{Binding mappingConnector.plugin}">
意思是当它在 DataTrigger 中用作 Setter 属性...
时,DP 似乎只有问题
尝试将静态命名为 属性 ChannelProperty
而不是 ChannelDP
。
在我的用户控件中:
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 中使用我的控件并使用数据触发器绑定到 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 中引发以下错误:
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),以下似乎工作正常:
<local:ChannelBox VerticalAlignment="Top" HorizontalAlignment="Left" Width="200" Channel="{Binding mappingConnector.plugin}">
意思是当它在 DataTrigger 中用作 Setter 属性...
时,DP 似乎只有问题尝试将静态命名为 属性 ChannelProperty
而不是 ChannelDP
。