简体   繁体   English

绑定到UserControl的属性不起作用

[英]Binding to property of UserControl doesn't work

I got a custom TextBox which I plan to include in another UserControl, however when setting up the Binding for it, it simply just doesn't bind. 我得到了一个自定义TextBox,打算将其包含在另一个UserControl中,但是为它设置Binding时,它只是不绑定。

I simplified the code for clarity. 为了简化起见,我简化了代码。

My custom TextBox: 我的自定义文本框:

<UserControl DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <TextBox Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}" />
</UserControl>
partial class CustomTextBox : UserControl 
{
    public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set
            {
                SetValue(TextProperty, value);
            }
        }

        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
            "Text",
            typeof(string),
            typeof(CustomTextBox),
            new PropertyMetadata(String.Empty));
}

This binding works as expected. 此绑定按预期方式工作。 When using CustomTextBox in another UserControl or Window, I can access the property just as expected. 在另一个UserControl或Window中使用CustomTextBox时,我可以按预期访问属性。

The following code blocks describe the UserControl that uses CustomTextBox and the corresponding ViewModel with the property I want to bind Text to. 下面的代码块描述了使用CustomTextBox的UserControl和具有要绑定Text的属性的相应ViewModel。

<UserControl>
    <UserControl.DataContext>
        <vm:MyViewModel />
    </UserControl.DataContext>
    <local:CustomTextBox Text="{Binding FooBar, UpdateSourceTrigger=PropertyChanged}" />
</UserControl>
public class MyViewModel : INotifyPropertyChanged
{
    private string _fooBar;
    public string FooBar
        {
            get { return _fooBar = (_fooBar ?? ""); }
            set
            {
                _fooBar = value; OnPropertyChanged();
            }
        }

    public event PropertyChangedEventHandler PropertyChanged;
}

My problem occurs exactly when I want to bind the Text property to a ViewModel in another UserControl, it just doesn't work. 当我想将Text属性绑定到另一个UserControl中的ViewModel时,确实发生了我的问题,但是它不起作用。 In this case I tried to bind the Text property to the FooBar property on the MyViewModel class, however changes to the Text property do not get reflected on the FooBar property and vice-versa. 在这种情况下,我尝试将Text属性绑定到MyViewModel类的FooBar属性,但是对Text属性的更改不会反映在FooBar属性上,反之亦然。 However when I hover over the binding in the XAML view, it shows the type of the property, so I don't exactly see what's wrong here. 但是,当我将鼠标悬停在XAML视图中的绑定上时,它显示了属性的类型,因此我没有确切地看到这里有什么问题。
My best guess is that it has to do with two bindings accessing the same property. 我最好的猜测是,它与访问同一属性的两个绑定有关。

modify DP registration to include FrameworkPropertyMetadataOptions.BindsTwoWayByDefault option 修改DP注册以包括FrameworkPropertyMetadataOptions.BindsTwoWayByDefault选项

public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
    "Text",
    typeof(string),
    typeof(CustomTextBox),
    new FrameworkPropertyMetadata(String.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

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

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