c# - 如何使用 iCommand 在 MainWindow 中更改我的 Textblock 中的文本?

标签 c# wpf mvvm data-binding

enter image description here

基本上我有一个文本 block 和 2 个按钮,我希望文本 block 文本根据我单击的按钮进行更改。例如,如果我单击 Button 1,它将显示“Button 1 is click”

如果我单击按钮 2,它将显示“按钮 2 是单击”

这是我的 View 模型

namespace ICommandProject2.ViewModel
{

    class ViewModel
    {
        public ICommand myCommand { get; set; }

        public ViewModel()
        {
            myCommand = new myCommand(ExecutedMethod);
        }

        private void ExecutedMethod (object parameter)
        {
            MainWindow m = new MainWindow();
            m.txtBlock.Text = "Button 1 is click";
        }


    }
}

这是我的命令类

命名空间 ICommandProject2.Command
{

    class myCommand : ICommand
    {

        Action<object> actionExecuted;

        public myCommand(Action<object> ExecutedMethod)
        {
            actionExecuted = ExecutedMethod;
        }

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            actionExecuted(parameter);
        }
    }
}

这是我的 Mainwindow.xaml
<Window x:Class="ICommandProject2.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:local="clr-namespace:ICommandProject2.ViewModel"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:ViewModel x:Key="vm"/>

    </Window.Resources>


    <Grid>
        <Button x:Name="btnOne" Content="Button 1" Command="{Binding myCommand, Source={StaticResource vm}}" HorizontalAlignment="Left" Margin="273,232,0,0" VerticalAlignment="Top" Width="75" FontSize="18"/>
        <TextBlock x:Name="txtBlock" HorizontalAlignment="Left" Margin="273,89,0,0" TextWrapping="Wrap" Text="This is a textblock" VerticalAlignment="Top" FontSize="36"/>
        <Button x:Name="btnTwo" Content="Button 2" HorizontalAlignment="Left" Margin="495,232,0,0" VerticalAlignment="Top" Width="75" FontSize="18" />

    </Grid>
</Window>

当我点击按钮时,什么也没有发生,我应该改变什么?

最佳答案

使用绑定(bind)设置 TextBlock 文本。

在 View 模型中创建一个属性来绑定(bind)并在命令处理程序中更改该属性:

class ViewModel : INotifyPropertyChanged
{
    public ICommand myCommand { get; set; }

    private string _title = "This is a textblock";
    public string Title { get { return _title; } }

    public ViewModel()
    {
        myCommand = new myCommand(ExecutedMethod);
    }

    private void ExecutedMethod (object parameter)
    {
        _title = "Button 1 was clicked";
        OnPropertyChanged("Title");
    }
}

我省略了那部分,但你应该实现 INotifyPropertyChanged通知 View 有关 View 模型中的更改。

在 View 中使用绑定(bind):
<TextBlock x:Name="txtBlock" Text="{Binding Title}" ...

(现在的命令不起作用,因为它创建了一个新的窗口实例( MainWindow m = new MainWindow(); )而不是使用打开的窗口)

关于c# - 如何使用 iCommand 在 MainWindow 中更改我的 Textblock 中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50076869/

相关文章:

c# - WPF DataGrid ItemsSource 问题

c# - 将 AutoCad 与 WPF C# 应用程序集成

带有彩色文本的 c# Console.WriteLine() 正在集中输出

wpf - ComboBox 在底部有额外的空间

swift - 使用 userdefault 保存并获取模型类的详细信息

wpf - 如何在 App.Xaml StartUpUri 中设置 View

xamarin - Xamarin Prism无法导航到LoginPageViewModel

c# - 确保 NHibernate SessionFactory 只创建一次

c# - 如何判断两个相似的乐队名称是否代表同一个乐队?

wpf - GridSplitter 未正确分割