在我的WPF应用中,我有一个带有依赖对象的自定义控件
public static readonly DependencyProperty SomeFieldProperty =
DependencyProperty.Register("SomeField", typeof(double), typeof(MyControl), new PropertyMetadata((double)0, OnPropChanged));
public double SomeField
{
get { return (double)GetValue(SomeFieldProperty ); }
set { SetValue(SomeFieldProperty , value); }
}
并且从我的XAML中,我试图像下面那样绑定到SomeField,但是它不起作用:更新:
<UserControl.Template>
<ControlTemplate TargetType="ContentControl">
<WrapPannel >
<abc:MyControl SomeField="{Binding SomeValue, Mode=TwoWay}" />
</WrapPannel>
</ControlTemplate>
</UserControl.Template>
[尝试了不同的解决方案,例如StackOverflow中的问题,但没有一个起作用。也许我的情况是特定的,因为我试图从模板中进行绑定?
看来这不是一个有约束力的问题。实际上,InitializeComponent()覆盖了UserControl的Template属性。这样的事情会起作用: