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

查看:41
本文介绍了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.

我没有做什么?如果您对此有任何帮助,我将不胜感激.

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 属性默认绑定为双向,则不需要指定 Mode:

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

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

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天全站免登陆