【问题标题】:Data Binding issue using NotificationBase使用 NotificationBase 的数据绑定问题
【发布时间】:2016-11-08 04:36:23
【问题描述】:
Custom_View.xaml

    <UserControl>
        <local:Custom_Text_Field
            Custom_Text_Field_Color="{x:Bind ViewModel.Color1 , Mode=TwoWay}">
        </local:Custom_Text_Field>
        <local:Custom_Text_Field
            Custom_Text_Field_Color="{x:Bind ViewModel.Color2 , Mode=TwoWay}">
        </local:Custom_Text_Field>
        <Button Click="{x:Bind ViewModel.ChangeColor"/>
    </UserControl>

Custom_View.cs

    public sealed partial class Custom_View : UserControl
    {
        public Custom_View_VM ViewModel { get; set; }
        public Custom_View()
        {
            ViewModel = new Custom_View_VM();
            this.InitializeComponent();
        }
    }

Custom_View_VM.cs

    public class Custom_View_VM : NotificationBase
    {
        public Brush Color1 { get; set; }
        public Brush Color2 { get; set; }
        public void  ChangeColor{//change color1 or color2};
    }

我使用了这个例子中的 NotificationBase 类:https://blogs.msdn.microsoft.com/johnshews_blog/2015/09/09/a-minimal-mvvm-uwp-app/

如果我影响构造函数中 Color1 或 Color2 的值,它会起作用(更改视图),但在调用 ChangeColor 后,视图模型中的值会更改,但不会影响视图。

【问题讨论】:

  • 通常你会在代码隐藏而不是 ViewModel 中创建 DependencyProperties。控件的使用者可以将控件的依赖属性绑定到它们的 ViewModel。
  • 感谢您的回答,我在后面的 Custom_Text_Field 代码中创建了 DependencyProperties,我为我的 Custom_View 创建了一个 ViewModel,因为 UI 比我的示例更复杂,并且他与来自 Web 服务的数据链接: (
  • 这里有 2 种不同类型的绑定:绑定到后面的代码(使用 Dependancy 属性)和绑定到 ViewModel(使用 INotifyPropertyChanged) - 不清楚哪一个不起作用:边框(使用DP)还是文本字段(绑定到 VM)?
  • Novitchi S 解决了我的问题,它是绑定到 VM =)
  • 我已经删除了依赖属性部分来澄清

标签: c# xaml mvvm data-binding inotifypropertychanged


【解决方案1】:

要更新 UI,它应该接收到 PropertyChanged 事件。您应该使用 NotificationBase 的机制来设置也会引发PropertyChanged 事件的属性:

public class Custom_View_VM : NotificationBase
{
    private Brush color1;
    public Brush Color1 
    {
        get { return color1; }
        set { SetProperty(color1, value, () => color1 = value); }
    }
    // TODO: same here
    public Brush Color2 { get; set; }
    public void  ChangeColor{//change color1 or color2};
}

此外,颜色通常不会进入ViewModelsViewModel 应该有一些业务逻辑属性,您可以将TextBox 的颜色作为XAML 的基础,例如IsNameAvailable

【讨论】:

  • 谢谢,但是得到 { return This.Color1; } 抛出一个 system.StackOverflowException :( 我真的很想念一些东西>
  • 修正了我的例子,不要使用来自NotificationBaseThis 字段,这太混乱了。您可以将其重命名为 Model 或其他名称。
  • 感谢它的工作!现在,我会跟着你,重构我的代码:)
【解决方案2】:

您需要注册该属性。

public static readonly DependencyProperty Custom_Text_Field_Color_Property =
        DependencyProperty.Register("Custom_Text_Field_Color", typeof(Brush), 
        typeof(Class_Name), new UIPropertyMetadata(null));

        public Brush Custom_Text_Field_Color
        {
            get { return (Brush)GetValue(Custom_Text_Field_Color_Property); }
            set { SetValue(Custom_Text_Field_Color_Property, value); }
        }

使用 typeof(Class_Name) 的控件名称(即类名称)。

【讨论】:

  • 感谢您的回答,但我已经在后面的 Custom_Text_Field 代码中注册了我的属性 :)
【解决方案3】:

如果类 NotificationBase 是一个自定义类,您可以使用 is 或不使用。

我基本上只解释了 MVVM 设计模式。 在 ViewModel 中,应该实现接口 INotifyPropertyChanged,并在设置属性时触发 PropertyChanged 事件。

public sealed class MainPageViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string _productName;
    public string ProductName
    {
        get { return _productName; }
        set
        {
            _productName = value;
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(ProductName)));
            }
        }
    }
}

下面的示例将演示这个 MVVM 设计模式。 https://code.msdn.microsoft.com/How-to-achieve-MVVM-design-2bb5a580

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-17
    • 2013-12-27
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    相关资源
    最近更新 更多