-2
为什么下面用“Binding”对象而不是字符串值填充我的依赖项属性?用Binding对象填充的依赖属性?
<local:CustomButton MyText="{Binding Name}" />
它与正常的按钮一起使用。
这是我如何注册我的按钮属性:
public static DependencyProperty RegisterProperty(string propertyName) {
return DependencyProperty.Register(propertyName, typeof(object), typeof(ButtonHD), new PropertyMetadata(null));
}
剩下的只是正常的样板代码。为什么在将名称绑定到MyText时获得Binding对象?我的意思是它与正常的按钮,这意味着绑定是好的。这非常奇怪。
完全实现我的用户控件:
public sealed partial class CustomButton : UserControl {
public object MyText {
get { return GetValue(MyTextDependency); }
set { SetValue(MyTextDependency, value); }
}
public static readonly DependencyProperty MyTextDependency = RegisterProperty(nameof(MyText));
public CustomButton() {
InitializeComponent();
}
public void Render(CanvasControl sender, CanvasDrawEventArgs e) {
var test = MyText;
// The below fails:
string test = (string) MyText;
}
public static DependencyProperty RegisterProperty(string propertyName) {
return DependencyProperty.Register(propertyName, typeof(object), typeof(CustomButton), new PropertyMetadata(null));
}
}
,在这里我的XAML:
<Canvas:CanvasControl Background="Transparent" Draw="Render">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="PointerReleased">
<Core:InvokeCommandAction Command="{Binding MyCommand}" />
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Canvas:CanvasControl>
你是怎么看的财产,并获得绑定的对象?你使用过'MyText.get' getter,还是像'ReadLocalValue'这样的一些奇特的东西? – quetzalcoatl
完全疯狂猜测:在VS中,检查该.xaml文件上的F4/Properties,并查看Build-Action是什么,并与可正常工作的其他xaml文件进行比较。可能有些东西像它没有被配置成View/Control/Page/etc,但是像resourcedictionary/etc这样的更特殊的组件类型,其中{Binding}不受支持,而XAML与IoCC一样工作,只是实例化对象并设置属性。另一个疯狂的猜测是:检查ButtonHD/CustomButton的.xaml文件,并查看根标记是否与':baseclass'(UserControl)相匹配 - 它必须与前缀/名称空间/ etc完全匹配。 – quetzalcoatl
^以上的猜测是基于观察你的代码看起来很好,所以除非你在其他地方做了一些奇怪的事情,否则XAML编译器似乎没有适当地编译XAML(即它不是'this.MyText = new Binding' 'this.SetBinding(MyTextProperty,new Binding')',或者基类不知何故不支持DependencyProperties /和/或绑定,他们把它当作正常值对象。 – quetzalcoatl