WPF 绑定到自定义控件中的自定义属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14628311/
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 07:19:29  来源:igfitidea点击:

WPF Binding to a custom property in a custom control

wpfbinding

提问by Duke Cyrillus

I have a custom text box defined as follows:

我有一个自定义文本框,定义如下:

public class CustomTextBox : TextBox
{
    public static DependencyProperty CustomTextProperty = 
             DependencyProperty.Register("CustomText", typeof(string), 
             typeof(CustomTextBox));

    static CustomTextBox()
    {
        TextProperty.OverrideMetadata(typeof(SMSTextBox),
                      new FrameworkPropertyMetadata(string.Empty,
                      FrameworkPropertyMetadataOptions.Journal |
                          FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                      new PropertyChangedCallback(CustomTextBox_OnTextPropertyChanged));
    }

    public string CustomText
    {
        get { return (string)GetValue(CustomTextProperty); }
        set { SetValue(CustomTextProperty, value); }
    }

    private static void CustomTextBox_OnTextPropertyChanged(DependencyObject d,
                     DependencyPropertyChangedEventArgs e)
    {
        CustomTextBox customTextBox = d as CustomTextBox;

        customTextBox.SetValue(CustomTextProperty, e.NewValue);
    }
}

I'm binding the Custom Text property in the XAML -

我在 XAML 中绑定自定义文本属性 -

<local:CustomTextBox CustomText="{Binding ViewModelProperty}" />

The problem I'm facing is that when I enter anything in the CustomTextBox, the changes are not reflected in the ViewModelProperty i.e. the ViewModelProperty is not getting updated. The CustomTextProperty is getting updated but I suppose I need to do something extra to make the binding work as well.

我面临的问题是,当我在 CustomTextBox 中输入任何内容时,更改不会反映在 ViewModelProperty 中,即 ViewModelProperty 没有得到更新。CustomTextProperty 正在更新,但我想我需要做一些额外的事情才能使绑定工作。

What am I not doing? I would appreciate any help regarding this.

我不做什么?我将不胜感激。

Thank you

谢谢

采纳答案by Clemens

I guess the binding needs to be two-way.

我想绑定需要是双向的。

<local:CustomTextBox
    CustomText="{Binding ViewModelProperty, Mode=TwoWay}" />

You wouldn't need to specify the Modeif you made the CustomTextproperty bind two-way by default:

Mode如果CustomText默认情况下将属性绑定为双向,则无需指定:

public static readonly DependencyProperty CustomTextProperty =
    DependencyProperty.Register(
        "CustomText", typeof(string), typeof(CustomTextBox),
        new FrameworkPropertyMetadata(
            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

You may also have to define a PropertyChangedCallback for the CustomTextproperty that updates the Textproperty (i.e. the other direction of what you have implemented now). Otherwise the TextBox won't display anything that is initially contained in the ViewModel property and of course woudln't be updated when the ViewModel property changes.

您可能还必须为CustomText更新Text属性的属性定义一个 PropertyChangedCallback (即您现在已实现的另一个方向)。否则 TextBox 将不会显示最初包含在 ViewModel 属性中的任何内容,当然也不会在 ViewModel 属性更改时更新。