我的财产未使用[CallerMemberName]更新 [英] My property does not update using [CallerMemberName]

查看:132
本文介绍了我的财产未使用[CallerMemberName]更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

诚然,我是wpf的新手.但是我花了一些时间在谷歌上搜索所有这些东西,但我很沮丧.

Admittedly I am new to wpf. But i have spent some time Googling about it all and I am stumped.

本质上,每当我的Model值更改时,我想使用Binding更新UI中的TextBlock.

in essence i want to update my TextBlock in my UI using Binding whenever my Model values change.

这是我的模特:

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

这是我的用户界面:

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

这是我后面的代码:

使用System.Windows;

using 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";
        }
    }
}

当我按下按钮时,UI上的文本不会更改..

When i press the button the text does not change on the UI..?

推荐答案

您在后面的代码中创建的实例与您在xaml中分配的实例不同.

The instance you create in the code behind is not the same as you assign in xaml.

将按钮单击事件更改为

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

然后将xaml中的内容绑定到

And the binding in xaml to

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

并且在您的视图模型中,您不会调用notify属性已更改.因此,创建一个私有字段并按如下所示修改属性.

And in your viewmodel you are not calling the notify property changed. 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); }
}

这篇关于我的财产未使用[CallerMemberName]更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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