Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用iCommand更改主窗口中我的文本块中的文本?_C#_Wpf_Mvvm_Data Binding - Fatal编程技术网

C# 如何使用iCommand更改主窗口中我的文本块中的文本?

C# 如何使用iCommand更改主窗口中我的文本块中的文本?,c#,wpf,mvvm,data-binding,C#,Wpf,Mvvm,Data Binding,基本上我有一个textblock和两个按钮,我希望textblock文本根据我单击的按钮进行更改。例如,如果我单击按钮1,它将显示“按钮1为单击” 如果我单击按钮2,它将显示“按钮2正在单击” 这是我的ViewModel namespace ICommandProject2.ViewModel { class ViewModel { public ICommand myCommand { get; set; } public ViewModel

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

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

这是我的ViewModel

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);
        }
    }
}
{
类myCommand:ICommand
{
已执行的行动;
公共myCommand(Action ExecutedMethod)
{
actionExecuted=ExecutedMethod;
}
公共事件处理程序CanExecuteChanged;
公共布尔CanExecute(对象参数)
{
返回true;
}
public void Execute(对象参数)
{
actionExecuted(参数);
}
}
}
这是我的主窗口.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>


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

使用绑定设置文本块文本

在视图模型中为绑定创建属性,并在命令处理程序中更改该属性:

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
来通知视图有关视图模型中的更改

在视图中使用绑定:

<TextBlock x:Name="txtBlock" Text="{Binding Title}" ...

使用绑定设置文本块文本

在视图模型中为绑定创建属性,并在命令处理程序中更改该属性:

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
来通知视图有关视图模型中的更改

在视图中使用绑定:

<TextBlock x:Name="txtBlock" Text="{Binding Title}" ...