简体   繁体   English

简单绑定问题

[英]simple binding problem

I have a rather simple problem, but I am unable to get my head around it... I have a class that inherits from UserControl . 我有一个相当简单的问题,但我无法解决……我有一个继承自UserControl的类。 It has an AxisColor DependencyProperty of type Color . 它具有类型ColorAxisColor DependencyProperty。 In the XAML structure of the class I have <ms3DTools:ScreenSpaceLines3D Thickness="2" Points="0,0,0 100,0,0" Color="{Binding Mode=OneWay, Path=AxisColor}" . 在该类的XAML结构中,我具有<ms3DTools:ScreenSpaceLines3D Thickness="2" Points="0,0,0 100,0,0" Color="{Binding Mode=OneWay, Path=AxisColor}" The binding does not work. 绑定无效。 What am I doing wrong? 我究竟做错了什么?

You probably want to add RelativeSource={RelativeSource Self} to the binding expression. 您可能想将RelativeSource={RelativeSource Self}到绑定表达式中。 Otherwise, you are binding to the DataContext property. 否则,您将绑定到DataContext属性。

If you want UI binding then you have to specify ElementName , more options here . 如果要进行UI绑定,则必须指定ElementName此处有更多选项。 If you want data binding then make sure you have DataContext set up correctly. 如果要进行数据绑定,请确保已正确设置DataContext。

If you have a UserControl that needs to get a Property AxisColor from a class, you need to set your DataContext of that UserControl to the class that contains AxisColor. 如果您有一个需要从类中获取属性AxisColor的UserControl,则需要将该UserControl的DataContext设置为包含AxisColor的类。

myUserControl.DataContext = myClassInstance;

Otherwise, your user control does not know where to grab the property from. 否则,您的用户控件将不知道从何处获取属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM