弹窗页面布局HandleView
<UserControl x:Class="WpfApp1.View.Dialog.HandleView"HandleViewModel:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp1.View.Dialog"
mc:Ignorable="d"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:system="clr-namespace:System;assembly=mscorlib"
d:DesignHeight="450" d:DesignWidth="800">
<StackPanel Margin="16">
<TextBox
VerticalAlignment="Center" Text="{Binding FormRFID}"
materialDesign:HintAssist.Hint="RFID标签" Margin="0,8,0,0"
HorizontalAlignment="Stretch"
Style="{StaticResource MaterialDesignFloatingHintTextBox}"
/>
<TextBox
VerticalAlignment="Center" Text="{Binding FormWeight}"
materialDesign:HintAssist.Hint="重量" Margin="0,8,0,0"
HorizontalAlignment="Stretch" materialDesign:HintAssist.HelperText="Helper text"
Style="{StaticResource MaterialDesignFloatingHintTextBox}"
/>
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal" Margin="10">
<Button Margin="0,8,8,0"
Content="添加"
IsDefault="True"
Command="{Binding SubmitCommand}"
Style="{StaticResource MaterialDesignFlatButton}"/>
<Button Margin="0,8,8,0"
Content="取消"
IsCancel="True"
Command="{Binding CancelCommand}"
Style="{StaticResource MaterialDesignFlatButton}"/>
</StackPanel>
</StackPanel>
</UserControl>
using CommunityToolkit.Mvvm.ComponentModel;调用:
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Contracts;
using MaterialDesignThemes.Wpf;
using Mod;
using Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO.Pipes;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media.Media3D;
using WpfApp1.Models;
namespace WpfApp1.ViewModels
{
public partial class HandleViewModel : ObservableObject, INotifyPropertyChanged
{
[ObservableProperty]
private string _formRFID;
[ObservableProperty]
private string _formWeight;
[ObservableProperty]
private string _msg;
// 添加命令 - 返回 true 表示确认
public IRelayCommand SubmitCommand { get; }
// 取消命令 - 返回 false 表示取消
public IRelayCommand CancelCommand { get; }
public HandleViewModel()
{
SubmitCommand = new RelayCommand(() =>
{
// 可以在这里添加验证逻辑
if (string.IsNullOrWhiteSpace(FormRFID))
{
MessageBox.Show("RFID不能为空");
return;
}
// 验证通过,关闭对话框并返回 true
DialogHost.CloseDialogCommand.Execute(true, null);
});
CancelCommand = new RelayCommand(() =>
{
// 直接关闭对话框并返回 false
DialogHost.CloseDialogCommand.Execute(false, null);
});
}
}
}
[RelayCommand]实现效果:
private async Task RunExtendedDialog()
{
//object? view = new HandleView
//{
// DataContext = _indexViewModel
//};
//object? result = await DialogHost.Show(new HandleView());
var dialogVm = new HandleViewModel();
var view = new HandleView { DataContext = dialogVm };
var result = await DialogHost.Show(view);
if (result as bool? == true) // 点击了添加
{
// 获取值
string rfid = dialogVm.FormRFID;
string weight = dialogVm.FormWeight;
// 处理业务逻辑
MessageBox.Show(rfid);
}
}
- 本文标题: WPF的MVVM怎么让他弹窗一个页面出来
- 文章分类:【WinForm/WPF】
- 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
- 上一篇:SQLServer之SQLClient和Dapper的主键错误区别捕获
- 下一篇: FTP推送