简体   繁体   English

我的财产未使用[CallerMemberName]更新

[英]My property does not update using [CallerMemberName]

Admittedly I am new to wpf. 诚然,我是wpf的新手。 But i have spent some time Googling about it all and I am stumped. 但是我花了一些时间在谷歌上搜索所有内容,但我感到很沮丧。

in essence i want to update my TextBlock in my UI using Binding whenever my Model values change. 本质上,每当我的Model值更改时,我想使用Binding更新UI中的TextBlock。

So this is my Model: 这是我的模型:

using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace WpfApplication1
{
    public class MyModel : INotifyPropertyChanged
    {
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (Equals(storage, value))
            {
                return false;
            }

            storage = value;
            this.OnPropertyChanged(propertyName);
            return true;
        }

        public string MyField { get; set ; } 
    }
}

This is my UI: 这是我的用户界面:

 <Window x:Class="WpfApplication1.MainWindow"
        xmlns:viewModels="clr-namespace:WpfApplication1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        d:DataContext="{d:DesignInstance viewModels:MyModel, IsDesignTimeCreatable=True}"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <viewModels:MyModel></viewModels:MyModel>
    </Window.DataContext>
    <Grid>
        <StackPanel Orientation="Vertical">
            <TextBlock Text="{Binding  MyModel.MyField}"></TextBlock> 
          <Button Content="Click Me!" Click="Button_Click" />  
        </StackPanel>
    </Grid>
</Window>

This is my code behind: 这是我的代码背后:

using System.Windows; 使用System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public MyModel myModel = new MyModel();


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            myModel.MyField = "has worked";
        }
    }
}

When i press the button the text does not change on the UI..? 当我按下按钮时,文本不会在UI上更改。

The instance you create in the code behind is not the same as you assign in xaml. 您在后面的代码中创建的实例与您在xaml中分配的实例不同。

Change the button click event to 将按钮单击事件更改为

private void Button_Click(object sender, RoutedEventArgs e)
{
    var model = this.DataContext as MyModel;
    model.MyField = "has worked";
}

And the binding in xaml to 并在xaml中绑定到

<TextBlock Text="{Binding MyField}"></TextBlock>

And in your viewmodel you are not calling the notify property changed. 并且在您的视图模型中,您不会调用notify属性已更改。 So create a private field and modify the property as below. 因此,创建一个私有字段并按如下所示修改属性。

private string myField;

public string MyField
{
    get { return this.myField; }
    set { this.SetProperty(ref this.myField, value); }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM