当前位置:   article > 正文

C# WPF Prism框架-2.MVVM的绑定和命令_c#的mvvm的代码

c#的mvvm的代码

WPF Prism框架-2.MVVM的绑定和命令

文本绑定

通过把属性改成 {Binding Title} 实现绑定 MainWindowViewModel 中的变量

MainWindow.xaml

<Window x:Class="demo1.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" Height="350" Width="525" >
    <Grid>
        <ContentControl prism:RegionManager.RegionName="ContentRegion" />
        <StackPanel>
            <TextBlock Text="123321"></TextBlock>
            <TextBox Text="{Binding Title}" Margin="10 20 30 10" FontSize="20"></TextBox>
            <TextBox Text="{Binding ShowText}"></TextBox>
        </StackPanel>
    </Grid>
</Window>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

MainWindowViewModel.cs

using Prism.Mvvm;

namespace demo1.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "Prism Application";

        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private string _ShowText = "none";

        public string ShowText
        {
            get { return _ShowText; }
            set { SetProperty(ref _ShowText, value); }
        }

        public MainWindowViewModel()
        {

        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

运行效果

在这里插入图片描述

绑定命令

MainWindow.xaml

<Window x:Class="demo1.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" Height="350" Width="525" >
    <Grid>
        <ContentControl prism:RegionManager.RegionName="ContentRegion" />
        <StackPanel>
            <TextBlock Text="123321"></TextBlock>
            <TextBox Text="{Binding Title}" Margin="10 20 30 10" FontSize="20"></TextBox>
            <TextBox Text="{Binding ShowText}"></TextBox>

            <!-- 按钮命令绑定 -->
            <Button Content="按钮1" Width="100" Command="{Binding BT1Command}"></Button>
            
        </StackPanel>
    </Grid>
</Window>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

MainWindowViewModel.cs


using Prism.Commands;
using Prism.Mvvm;
using System.Windows;

namespace demo1.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "Prism Application";
        private string _ShowText = "none";

        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        public string ShowText
        {
            get { return _ShowText; }
            set { SetProperty(ref _ShowText, value); }
        }

        private DelegateCommand _BT1Command;
        public DelegateCommand BT1Command =>
            _BT1Command ?? (_BT1Command = new DelegateCommand(ExecuteBT1Command));

        void ExecuteBT1Command()
        {
            MessageBox.Show("123", "321");
        }

        public MainWindowViewModel()
        {

        }
    }
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

运行效果

在这里插入图片描述

绑定命令 + 操作文本

MainWindow.xaml 还是上面例子的 MainWindow.xaml

MainWindowViewModel.cs


using Prism.Commands;
using Prism.Mvvm;
using System.Windows;

namespace demo1.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "Prism Application";
        private string _ShowText = "none";

        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        public string ShowText
        {
            get { return _ShowText; }
            set { SetProperty(ref _ShowText, value); }
        }

        private DelegateCommand _BT1Command;
        public DelegateCommand BT1Command =>
            _BT1Command ?? (_BT1Command = new DelegateCommand(ExecuteBT1Command));

        void ExecuteBT1Command()
        {
            MessageBox.Show(Title, "321");
            ShowText = Title;
        }

        public MainWindowViewModel()
        {

        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

运行效果

在这里插入图片描述

可以看到,现在输入框里面输入一些文字,然后点击按钮1,弹出框显示输入的文字,然后下面的TextBox也显示输入的文字

在这里插入图片描述

绑定List对象

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/746026
推荐阅读
相关标签
  

闽ICP备14008679号