WPF 简单文本框绑定 - 从未触及依赖属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13739441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:30:37  来源:igfitidea点击:

WPF Simple TextBox Binding - Dependency Property Never Touched

wpfbindingtextboxdependency-properties

提问by jamesrom

I have a TextBoxand I'm trying to bind it to a DependencyProperty. The property is never touched on load or when I type in the TextBox. What am I missing?

我有一个TextBox,我正试图将它绑定到一个DependencyProperty. 该属性在加载时或当我输入TextBox. 我错过了什么?

XAML

XAML

<UserControl:Class="TestBinding.UsernameBox"
        // removed xmlns stuff here for clarity>
    <Grid>
        <TextBox Height="23" Name="usernameTextBox" Text="{Binding Path=Username, ElementName=myWindow, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
    </Grid>
</UserControl>

C#

C#

public partial class UsernameBox : UserControl
{
    public UsernameBox()
    {
        InitializeComponent();
    }

    public string Username
    {
        get
        {
            // Control never reaches here
            return (string)GetValue(UsernameProperty);
        }
        set
        {
            // Control never reaches here
            SetValue(UsernameProperty, value);
        }
    }

    public static readonly DependencyProperty UsernameProperty
        = DependencyProperty.Register("Username", typeof(string), typeof(MainWindow));
}

Edit: I need to implement a DependencyPropertybecause I am creating my own control.

编辑:我需要实现一个,DependencyProperty因为我正在创建自己的控件。

回答by Eugene Cheverda

You never reach setter because it is CLR wrapper of dependency property, it is declared to be set from external sources, like mainWindow.Username = "myuserName";. When the property is set through binding and you want to see if it changed or not just add the PropertyMetadatato your declaration with PropertyChangedCallback, for example:

你永远不会到达 setter 因为它是依赖属性的 CLR 包装器,它被声明为从外部源设置,例如mainWindow.Username = "myuserName";. 当通过绑定设置属性并且您想查看它是否更改时,只需将 添加PropertyMetadata到您的声明中PropertyChangedCallback,例如:

public static readonly DependencyProperty UsernameProperty =
            DependencyProperty.Register("Username", typeof(string), typeof(MainWindow), new UIPropertyMetadata(string.Empty, UsernamePropertyChangedCallback));

        private static void UsernamePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Debug.Print("OldValue: {0}", e.OldValue);
            Debug.Print("NewValue: {0}", e.NewValue);
        }

With this code you will see changes of your property in Output Window of VS.

使用此代码,您将在 VS 的输出窗口中看到您的属性的更改。

For more info on callbacks please read Dependency Property Callbacks and Validation

有关回调的更多信息,请阅读依赖属性回调和验证

Hope this helps.

希望这可以帮助。

回答by markmnl

You shouldn't be using a DependencyPropertyhere!

你不应该在DependencyProperty这里使用!

Your TextBox's Textproperty is a DependencyPropertyand is the targetof your binding your Username Property is the source and should notbe a DependencyPropertyas well! It should be a plain old property that raises NotifyPropertyChanged.

你的文本框的Text属性是一个DependencyProperty并且是目标的你结合你的用户名属性是源,应该不会DependencyProperty也!它应该是引发NotifyPropertyChanged的普通旧属性。

All you need is:

所有你需要的是:

private string _username;
public string Username
{
    get
    {
        return _username;
    }
    set
    {
        _username = value;
         NotifyPropertyChanged("Username");
    }
}

(Aside: you only need to use DependencyProperties when you are authoring your own control.)

(另外:当您创作自己的控件时,您只需要使用 DependencyProperties。)