【问题标题】:How to bind a dependency property for a custom control to its view models property如何将自定义控件的依赖属性绑定到其视图模型属性
【发布时间】:2012-10-05 23:44:07
【问题描述】:

我正在尝试将自定义控件的依赖属性绑定到其 ViewModel 的属性。

自定义控件如下所示:


    public partial class MyCustomControl : Canvas
    {
            //Dependency Property
            public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MyCustomControl));


            private VisualCollection controls;
            private TextBox textBox;

            public string Text
            {
                get { return textBox.Text; }
                set 
                {
                    SetValue(TextProperty, value);
                    textBox.Text = value;
                }
            }

            //Constructor
            public MyCustomControl ()
            {
                controls = new VisualCollection(this);
                InitializeComponent();

                textBox = new TextBox();
                textBox.ToolTip = "Start typing a value.";

                controls.Add(textBox);

                //Bind the property
                this.SetBinding(TextProperty, new Binding("Text") {Mode = BindingMode.TwoWay, Source = DataContext});
            }
   }

View Model 看起来像:


-------

public class MyCustomControlViewModel: ObservableObject
{
    private string _text;


    public string Text
    {
        get { return _text; }
        set { _text = value; RaisePropertyChanged("Text");}
    }
}

----------

“文本”属性的此绑定由于某种原因无法正常工作。

我想要做的是,在实际实现中,我希望 MyCustom Control 的 text 属性在我更新底层 ViewModel 的 Text 属性时更新。

非常感谢您对此提供任何帮助。

【问题讨论】:

  • 显示您正在使用的 xaml 绑定。
  • 似乎应该可以,在文本设置器中设置一个断点
  • @eranotzer :我确实尝试在文本设置器上放置一个断点,但它从未触及视图中的设置器,原因是属性未绑定到底层视图模型属性。
  • @MichaelG :视图中没有 xaml 绑定,因为所有控件都是动态生成的。视图的 Xaml 非常小:'schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft.com/winfx/2006/xaml"> '
  • @MichaelG :但是使用控件的 xaml 看起来像:'' 我在代码中确认控件的 datacontext 属性成功绑定到 MyCustomControlVM 属性,即 MyCustomControlViewaModel 的实例。此时无法将控件属性绑定到底层视图模型的属性。

标签: c# wpf mvvm binding viewmodel


【解决方案1】:

经过一番研究,我终于找出了我的代码的问题。我通过创建一个静态事件处理程序来使这段代码正常工作,该处理程序实际上将新属性值设置为依赖属性的基础公共成员。依赖属性声明如下:

//Dependency Property
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MyCustomControl), new PropertyMetadata(null, OnTextChanged));

然后定义设置属性的静态方法是这样的:

private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    MyCustomControl myCustomControl = (MyCustomControl)d;
    myCustomControl.Text = (string) e.NewValue;
}

这是我唯一缺少的东西。

干杯

【讨论】:

    【解决方案2】:

    只需绑定到依赖属性

    <MyCustomControl Text="{Binding Path=Text}" />
    

    【讨论】:

    • 我想将数据上下文绑定到底层视图模型,然后我希望我的控件属性绑定能够工作。像这样的东西:'
    【解决方案3】:

    您应该将您的成员 TextBox 绑定到您的 TextProperty。我很确定您的 Text 属性上的 xaml 中的绑定会覆盖您在构造函数中所做的绑定。

    【讨论】:

      猜你喜欢
      • 2012-08-29
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多