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

用户名

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

请帮忙 !!

程序视图:

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

查看模型

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

    }
}
}
罗希特·瓦茨

更换

RaisePropertyChanged(MyText);

RaisePropertyChanged("MyText");

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

绑定动态生成的文本块WPF MVVM Light

来自分类Dev

将数据绑定到WPF格式MVVM

来自分类Dev

WPF DataGrid借助mvvm light绑定到实体框架

来自分类Dev

WPF列表框将ItemsSource与MVVM-light绑定

来自分类Dev

WPF MVVM数据绑定

来自分类Dev

MVVM Light DataGrid绑定

来自分类Dev

WPF MVVM Light启用按钮(如果选择了数据网格行)

来自分类Dev

数据网格上的WPF MVVM Light动态列表源

来自分类Dev

数据网格上的WPF MVVM Light动态列表源

来自分类Dev

WPF MVVM:绑定到对象的属性

来自分类Dev

C#WPF绑定不从属性获取数据

来自分类Dev

C#WPF绑定不从属性获取数据

来自分类Dev

将ICommand绑定到TabItem WPF(MVVM)

来自分类Dev

WPF MVVM:将命令绑定到事件

来自分类Dev

WPF MVVM绑定嵌套属性

来自分类Dev

WPF MVVM绑定嵌套属性

来自分类Dev

如何使用 UWP 和 MVVM Light 刷新 Telerik 数据网格

来自分类Dev

MVVM WPF中的数据绑定

来自分类Dev

MVVM WPF中的数据绑定

来自分类Dev

mvvm light wpf取消注册

来自分类Dev

WPF ComboBox Mvvm绑定

来自分类Dev

WPF mailto MVVM绑定

来自分类Dev

MVVM Light View 不刷新绑定属性

来自分类Dev

WPF MVVM Light数据绑定上下文菜单以列出视图项

来自分类Dev

将mvvm light中继命令绑定到页面加载事件

来自分类Dev

具有MVVM和用户控件的WPF数据绑定

来自分类Dev

具有MVVM和用户控件的WPF数据绑定

来自分类Dev

WPF mvvm light 4.5 ListBox SelectedItem两种方式绑定损坏

来自分类Dev

如何使用WPF和MVVM将命令绑定到Window Loaded事件