在我的用户控件中:
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 中引发以下错误:
属性“Channel”不是 DependencyProperty。用于
标记,非附加属性必须在目标类型上公开
具有可访问的实例属性“Channel”。对于附加的
属性,声明类型必须提供静态“GetChannel”和
“设置频道”
方法。
但与错误让我相信的相反(我以某种方式设置了 DP 错误),以下内容似乎工作正常:
<local:ChannelBox VerticalAlignment="Top" HorizontalAlignment="Left" Width="200" Channel="{Binding mappingConnector.plugin}">
这意味着当 DP 在 DataTrigger 中用作 Setter 属性时,它似乎仅存在问题...