1.自定义对话框xaml
<UserControl x:Class="NavigationApp.Views.MyDialog" 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:NavigationApp.Views" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <TextBlock Text="编辑" FontSize="22"/> <TextBox Grid.Row="1" Text="{Binding Title}" Height="30" Margin="15"/> <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right"> <Button Content="OK" Margin="5" Padding="3" FontSize="22" Command="{Binding SaveCommand}"/> <Button Content="Cancel" Margin="5" Padding="3" FontSize="22" Command="{Binding CancelCommand}"/> </StackPanel> </Grid> </UserControl>View Code
2.对话框ViewModel
public class MyDialogViewModel : BindableBase, IDialogAware { public MyDialogViewModel() { SaveCommand = new DelegateCommand(() => { DialogParameters keyValuePairs = new DialogParameters(); keyValuePairs.Add("value", Title); RequestClose?.Invoke(new DialogResult(ButtonResult.OK, keyValuePairs)); }); CancelCommand = new DelegateCommand(() => { RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); ; }); } public DelegateCommand SaveCommand { get; set; } public DelegateCommand CancelCommand { get; set; } private string title; public string Title { get { return title; } set { title = value; RaisePropertyChanged("Title"); } } public event Action<IDialogResult> RequestClose; public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { Title = parameters.GetValue<string>("value"); } }
3.调用对话框
//App.cs中注册 containerRegistry.RegisterDialog<MyDialog, MyDialogViewModel>("Question"); //显示 private void ShowDLG() { DialogParameters pairs = new DialogParameters(); pairs.Add("value", "Test1"); dialog.ShowDialog("Question", pairs, arg => { if (arg.Result == ButtonResult.OK) { var value = arg.Parameters.GetValue<string>("value"); MessageBox.Show(value); } else { MessageBox.Show("取消了"); } }); }
标签:DelegateCommand,Title,DialogParameters,value,Dialog,new,public From: https://www.cnblogs.com/chixiner/p/17234808.html