XAML:绑定到模板中的自定义控件的属性

问题描述 投票:0回答:2

在我的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中的问题,但没有一个起作用。也许我的情况是特定的,因为我试图从模板中进行绑定?

wpf xaml wpf-controls xaml-binding
2个回答
0
投票

看来这不是一个有约束力的问题。实际上,InitializeComponent()覆盖了UserControl的Template属性。这样的事情会起作用:


0
投票
© www.soinside.com 2019 - 2024. All rights reserved.