WPF之MVVM框架 实现属性绑定,命令绑定,事件触发

从Nuget中下载包

1、mvvmlights

2、propertyChanged(需要加载)并继承inotyporpertychanged

3、System.Windows.Interactivity.WPF

创建文件夹

ViewModel文件夹

新建文本ViewModelLocator

//导入命名空间
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;

   public ViewModelLocator()
   {
       SimpleIoc.Default.Register<MainViewModel>();
   }
   public MainViewModel Main
   {
        get { return SimpleIoc.Default.GetInstance<MainViewModel>(); }
   }

新建界面的ViewModel文件

using galasoft.Mvvmlight
public class MainViewModel:ViewModelBase
 {

 }

修改app.xaml

///引入 viewmodellocator的命名空间 
 xmlns:vm="clr-namespace:YanTai.LB.Excel.ViewModel" 
<ResourceDictionary>
     <vm:ViewModelLocator x:Key="Locator"/>
</ResourceDictionary>

修改View

//方法1:需要配置app.xml
 <Window.DataContext>
        <Binding Path="Main" Source="{StaticResource Locator}"/>
  </Window.DataContext>
//方法2:不需要配置app.xml
  xmlns:vm="clr-namespace:textmvvm.ViewModel"
 <Window.DataContext>
        <vm:MainViewModel />
    </Window.DataContext>

新建Model

使用model类可以方便后期的调用也简化了后期的代码量

自定义类字段可以继承于observableobject,最好都要继承

///引入命名空间
using GalaSoft.MvvmLight;
public class MainModel:ObservableObject
{
     public string Title { set; get; }
     public string Submit { set; get; }
public Studen student{set;get;}
}

public class Studen:INotifyPropertyChanged
{

public string Name{set;get;}
public string Age{set;get;}
}

需要导入dllpropertyChanged。

引入窗体事件

  xmlns:intr="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"  
<intr:Interaction.Triggers>
    <intr:EventTrigger EventName="KeyDown">
    <intr:InvokeCommandAction Command="{Binding TB_MouseDown }"
  </intr:EventTrigger>
 </intr:Interaction.Triggers>

输入框中的按键指令和鼠标操作可采用如下方式

 <TextBox x:Name="TB_ProductType"
                             Grid.Column="1"
                             BorderThickness="0,0,0,2"
                             FontSize="20"
                             VerticalContentAlignment="Bottom"
                             Text="{Binding Model.product.Product_Type}">
                        
  <TextBox.InputBindings>
       <KeyBinding Command="{Binding TB_MouseDown}"  
                            Key="Enter" 
                            CommandParameter="{Binding ElementName=TB_ProductType,                 Path=Text}"/>
  </TextBox.InputBindings>
 </TextBox>
<border>
<contentcontrol content="{binding usercontrol"/>

</border>
在viewModel中需要引入using system.windows.control
然后和属性一样绑定

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我会尽力回答你的问题。 首先,MVVM框架是一种用于构建WPF应用程序的设计模式,它将应用程序分为三个部分:模型(Model)、视图(View)和视图模型(ViewModel)。模型是应用程序的数据和业务逻辑,视图是用户界面,而视图模型是连接模型和视图的中介。 接下来,我们来实现鼠标操作画圆和旋转矩形的功能。首先,在视图模型中创建两个命令,一个用于画圆,一个用于旋转矩形。在命令的Execute方法中,我们可以通过鼠标事件获取到鼠标的位置,然后根据这个位置绘制圆或者矩形。 下面是一个简单的示例代码: ```csharp public class MainViewModel : INotifyPropertyChanged { public ICommand DrawCircleCommand { get; set; } public ICommand RotateRectangleCommand { get; set; } private Point _lastPosition; private ObservableCollection<Shape> _shapes = new ObservableCollection<Shape>(); public ObservableCollection<Shape> Shapes { get { return _shapes; } set { _shapes = value; OnPropertyChanged(nameof(Shapes)); } } public MainViewModel() { DrawCircleCommand = new RelayCommand(DrawCircle); RotateRectangleCommand = new RelayCommand(RotateRectangle); } private void DrawCircle(object obj) { var args = obj as MouseEventArgs; if (args != null) { var position = args.GetPosition(null); var radius = 50; var circle = new Ellipse { Width = radius, Height = radius, Fill = Brushes.Red }; Canvas.SetLeft(circle, position.X - radius / 2); Canvas.SetTop(circle, position.Y - radius / 2); Shapes.Add(circle); } } private void RotateRectangle(object obj) { var args = obj as MouseEventArgs; if (args != null) { var position = args.GetPosition(null); var width = 100; var height = 50; var rectangle = new Rectangle { Width = width, Height = height, Fill = Brushes.Green, RenderTransformOrigin = new Point(0.5, 0.5) }; Canvas.SetLeft(rectangle, position.X - width / 2); Canvas.SetTop(rectangle, position.Y - height / 2); var transform = new RotateTransform { Angle = 0 }; rectangle.RenderTransform = transform; Shapes.Add(rectangle); var animation = new DoubleAnimation { From = 0, To = 360, Duration = new Duration(TimeSpan.FromSeconds(5)), RepeatBehavior = RepeatBehavior.Forever }; transform.BeginAnimation(RotateTransform.AngleProperty, animation); } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } public class RelayCommand : ICommand { private Action<object> _execute; private Func<object, bool> _canExecute; public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null) { _execute = execute; _canExecute = canExecute; } public bool CanExecute(object parameter) { return _canExecute == null || _canExecute(parameter); } public void Execute(object parameter) { _execute(parameter); } public event EventHandler CanExecuteChanged; public void RaiseCanExecuteChanged() { CanExecuteChanged?.Invoke(this, EventArgs.Empty); } } ``` 在这个示例代码中,我们创建了一个MainViewModel类,其中包含了两个命令DrawCircleCommand和RotateRectangleCommand,分别用于绘制圆和旋转矩形。当用户在画布上单击鼠标时,会触发对应的命令,并通过鼠标事件获取到鼠标位置,然后创建相应的形状并添加到Shapes集合中。可以通过绑定Shapes集合到画布上,将绘制的形状显示在界面上。 以上是一个简单的实现,具体的实现方式可以根据自己的需求进行调整和优化。希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

工控匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值