WPF绑定到一个自定义属性在自定义控件 [英] WPF Binding to a custom property in a custom control

查看:177
本文介绍了WPF绑定到一个自定义属性在自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义为自定义文本框如下:

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);
    }
}

我结合自定义文本属性在XAML -

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

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

我面临的问题是,当我在CustomTextBox输入任何内容,更改不会反映在ViewModelProperty即ViewModelProperty是没有得到更新。该CustomTextProperty正在得到更新,但我想我需要做些额外的事情,使结合工作也是如此。

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.

什么是我不能做什么?我想AP preciate对此的任何帮助。

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

感谢您

推荐答案

我想绑定需要是双向的。

I guess the binding needs to be two-way.

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

您将不需要指定模式如果您在默认情况下做出的 CustomText 属性绑定双向

You wouldn't need to specify the Mode if you made the CustomText property bind two-way by default:

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

您可能还需要定义一个PropertyChangedCallback该更新 CustomText 属性Text 属性(即其他你现在已经实现什么方向)。否则,文本框将不显示任何东西,最初包含在视图模型属性,当然woudln't被更新时,视图模型属性更改。

You may also have to define a PropertyChangedCallback for the CustomText property that updates the Text property (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.

这篇关于WPF绑定到一个自定义属性在自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆