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