将数据从属性绑定到文本块 - MVVM Light 和 WPF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19888740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:01:48  来源:igfitidea点击:

Bind Data From Property to Textblock - MVVM Light and WPF

c#wpfxamlmvvmmvvm-light

提问by user2975939

I have a textblock in WPF which is bound to a property in my ViewModel class. On click of a button I wish to modify the property and expect the same to be reflected in my textblock. I want all these to be done purely using MVVM (MVVMLight). I am using MMVM light and VS 2012. Challenges- On button click the changes are not being reflected. Though the program execution is going inside the property , changes are not being made.

我在 WPF 中有一个文本块,它绑定到我的 ViewModel 类中的一个属性。单击按钮时,我希望修改该属性,并希望在我的文本块中反映相同的内容。我希望所有这些都完全使用 MVVM (MVVMLight) 完成。我正在使用 MMVM light 和 VS 2012。挑战 - 单击按钮时,更改未得到反映。尽管程序在属性内部执行,但并未进行更改。

Please Help !!

请帮忙 !!

Program- View:

程序视图:

<Window x:Class="MvvmLight1_Trail.MainWindow"
    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:ignore="http://www.ignore.com"
    mc:Ignorable="d ignore"
    Height="500"
    Width="500"
    Title="MVVM Light Application"
    DataContext="{Binding Main, Source={StaticResource Locator}}">



<Grid x:Name="LayoutRoot">


    <TextBlock FontSize="34"
               Text="{Binding Path=MyText,UpdateSourceTrigger=Default, Mode=TwoWay}"
               VerticalAlignment="Center"
               HorizontalAlignment="Center"
               TextWrapping="Wrap" />
    <Button Width="100" Height="100" Command="{Binding PressCommand}" Margin="198.985,277.537,193.014,92.462" Content="Press Me"/>

</Grid>

View Model

查看模型

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MvvmLight1_Trail.Model;
using System.ComponentModel;
using System.Threading;

namespace MvvmLight1_Trail.ViewModel
{

public class MainViewModel : ViewModelBase
{



    public RelayCommand PressCommand { get; private set; }
    Thread t;
    private string _welcomeTitle = string.Empty;


    public string MyText
    {
        get
        {
            return _welcomeTitle;
        }

        set
        {
            if (_welcomeTitle == value)
            {
                return;
            }

            _welcomeTitle = value;
            RaisePropertyChanged(MyText);
        }
    }

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {
        PressCommand = new RelayCommand(() => MyFunc());
        myfunc();
    }

    private void MyFunc()
    {
        this.MyText = "Hi2";
    }

    private void myfunc()
    {

        this.MyText = "Hello";
        this.MyText = "Hi";

    }
}
}

回答by Rohit Vats

Replace

代替

RaisePropertyChanged(MyText);

RaisePropertyChanged(MyText);

to

RaisePropertyChanged("MyText");

RaisePropertyChanged("MyText");

PropertyChanged event should be raised on property name and not on property value.

PropertyChanged 事件应该在属性名称而不是属性值上引发。

回答by arpl

Already answered by @Rohit Vats. You can also call RaisePropertyChanged like, RaisePropertyChanged( () => MyText)to ease renaming later.

@Rohit Vats 已经回答了。您还可以调用 RaisePropertyChanged 之类的方法,RaisePropertyChanged( () => MyText)以便稍后重命名。

回答by barakcaf

Late to the game but:

比赛迟到了,但是:

in new C# 6 you can also use nameof like this:

在新的 C# 6 中,您还可以像这样使用 nameof:

RaisePropertyChanged(nameof(MyText))