Binding preference: Name or Type?
knowing that MyControl
has a depProp. PX1
, what binding should I prefeer, line1
or line2
?
<UserControl x:Class="MyProject.MyControl"
xmlns:my="clr-namespace:MyProject"
x:Name="ParentControl">
<Canvas>
<Line x:Name="line1"
X1="{Binding RelativeSource={RelativeSource AncestorType={x:Type my:MyControl}}, Path=PX1}" />
<Line x:Name="line2"
X1="{Binding ElementName=ParentControl, Path=PX1}" />
</Canvas>
</UserControl>
I mean, should开发者_如何学JAVA we ensure the uniqueness of the name "ParentControl" per all possible parents in the second case?
I would prefer the latter syntax in most cases, as it's easier to read the intent, as long as you choose a clear name for the ParentControl. The down side is that this fails if you change the name, whereas the first will continue to work.
Side Note: PX1 doesn't actually need to be a DP, as long as the UserControl implements INotifyPropertyChanged and notifies when PX1 changes.
精彩评论