c# - 如何从UserControl1 WPF MVVM中的按钮将数据从一个UserControl1传递到UserControl2

标签 c# wpf xaml mvvm

我最近开始研究用户控件。我有一个包含这 2 个用户控件的主窗口。 UserControl1 是一个获取数据的表单,当在 UserControl1 中单击提交按钮时,将可以看到 UserControl2 查看从 UserControl1 发送的数据。

这是我的 MainWindow xaml (HomeCareMain.xaml)

    <Window x:Class="PatientRecordMVVM.Views.HomeCareMain"
        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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        FontFamily="{materialDesign:MaterialDesignFont}"
        xmlns:local="clr-namespace:PatientRecordMVVM.Views"
        mc:Ignorable="d"
        Title="HomeCareMain" Height="850" Width="1500" Foreground="White">
    <Window.Resources>
        <Storyboard x:Key="MenuOpen">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="GridSideMenu">
                <EasingDoubleKeyFrame KeyTime="0" Value="60"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0:0" Value="210"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="MenuClose">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="GridSideMenu">
                <EasingDoubleKeyFrame KeyTime="0" Value="210"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0:0" Value="60"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>

    <Window.Triggers>
        <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="ButtonOpenMenu">
            <BeginStoryboard Storyboard="{StaticResource MenuOpen}"/>
        </EventTrigger>
        <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="ButtonCloseMenu">
            <BeginStoryboard Storyboard="{StaticResource MenuClose}"/>
        </EventTrigger>
    </Window.Triggers>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3*"/>
            <ColumnDefinition Width="8*"/>
            <ColumnDefinition Width="8*"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="25*"/>
        </Grid.RowDefinitions>

        <Image x:Name="Logo" Grid.Row="1" Grid.ColumnSpan="3"  Source="/PatientRecordMVVM;component/Images/logo.jpg" Opacity="0.12" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        <Grid Grid.Row="0" Grid.ColumnSpan="3" Height="60" VerticalAlignment="Top" Background="#2c8a93">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                <Image Source="/PatientRecordMVVM;component/Images/logo.jpg" Width="30" Height="30" Margin="0,0,20,0"/>
                <TextBlock Text="HOME CARE" FontSize="22" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </StackPanel>
            <materialDesign:PopupBox PlacementMode="BottomAndAlignRightEdges" StaysOpen="False" Foreground="White" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="10">
                <StackPanel Width="150">
                    <Button Content="Settings"/>
                    <Separator/>
                    <Button Command="{Binding Path=LogoutCommand}" Content="Logout"/>
                </StackPanel>
            </materialDesign:PopupBox>
        </Grid>

        <Grid x:Name="GridSideMenu" Grid.RowSpan="2" Grid.Column="0" Width="210" HorizontalAlignment="Left" Background="#1f3e66">
            <StackPanel Orientation="Vertical">
                <Grid Height="60" VerticalAlignment="Top">
                    <Button x:Name="ButtonCloseMenu" Background="{x:Null}" BorderBrush="{x:Null}" Width="60" Height="60" HorizontalAlignment="Right" Visibility="Collapsed" Click="ButtonCloseMenu_Click">
                        <materialDesign:PackIcon Kind="ArrowLeft" Width="25" Height="25"/>
                    </Button>
                    <Button x:Name="ButtonOpenMenu" Background="{x:Null}" BorderBrush="{x:Null}" Width="60" Height="60" HorizontalAlignment="Right" Click="ButtonOpenMenu_Click">
                        <materialDesign:PackIcon Kind="Menu" Width="25" Height="25"/>
                    </Button>
                </Grid>
                <ListView Foreground="White" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                    <ListViewItem Height="60">
                        <Button Command="{Binding Path=AddPatientCommand}" CommandParameter="{Binding ElementName=AddPatient}" Background="#1f3e66" BorderBrush="#1f3e66" Width="190" Height="40" VerticalAlignment="Top">
                            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                <materialDesign:PackIcon Kind="Add" Foreground="White" Width="25" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="-5,0,20,0"/>
                                <TextBlock Text="Add Patient Details" FontSize="12" VerticalAlignment="Center" Margin='0,0,20,0'/>
                            </StackPanel>
                        </Button>
                    </ListViewItem>
                </ListView>
            </StackPanel>
        </Grid>

        <Grid Grid.Row="1" Grid.ColumnSpan="3">
            <local:AddPatientRecordDetails 
                x:Name="AddPatient" 
                HorizontalAlignment="Stretch"
                VerticalAlignment="Center"
                Width="800"
                Height="730"
                Margin="0,0,20,0"
             />
        </Grid>

        <Grid Grid.Row="1" Grid.Column="2">
            <local:PrintPreviewControl
                x:Name="PrintPreview"
                HorizontalAlignment="Center"
                Width="600"
                Height="700"
                />
        </Grid>
    </Grid>
</Window>

这是 UserControl1 xaml (AddPatientRecordDetails.xaml)
<UserControl x:Class="PatientRecordMVVM.Views.AddPatientRecordDetails"
             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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             FontFamily="{materialDesign:MaterialDesignFont}"
             xmlns:local="clr-namespace:PatientRecordMVVM.Views"
             mc:Ignorable="d" 
             d:DesignHeight="850" d:DesignWidth="600" Background="Transparent" BorderBrush="#58af9d" BorderThickness="1">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2.5*"/>
            <ColumnDefinition Width="3*"/>
            <ColumnDefinition Width="2.5*"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="1.5*"/>
            <RowDefinition Height="1.5*"/>
            <RowDefinition Height="18*"/>
            <RowDefinition Height="2*"/>
        </Grid.RowDefinitions>

        <Grid Grid.RowSpan="2" Grid.ColumnSpan="3" Background="#2c8a93"/>

        <!--Patient Id block-->
        <StackPanel Grid.ColumnSpan="2" Style="{StaticResource StackpanelStyle}">
            <Label FontSize="16" FontWeight="Bold" Foreground="White">
                Patient ID :
            </Label>
            <Label Content="{Binding Path=GuidGenerator}" FontSize="16" Foreground="White" Margin="5,0"/>
        </StackPanel>

        <!--Date block-->
        <StackPanel  Grid.ColumnSpan="3"  Style="{StaticResource StackpanelStyle}" HorizontalAlignment="Right">
            <Label FontSize="16" FontWeight="Bold" Foreground="White">
                Date :
            </Label>
            <Label Name="date_time" Content="{Binding Path=CurrentDate}" FontSize="16" Foreground="White" Margin="5,0"/>
        </StackPanel>

        <!--Title block-->
        <TextBlock Grid.Row="1" Grid.ColumnSpan="3" FontSize="20" FontWeight="Bold" Foreground="White" TextDecorations="Underline" HorizontalAlignment="Center" VerticalAlignment="Center" MinWidth="200">
                Patient Registration Information
        </TextBlock>

        <!--Main sub Grid-->
        <Grid Grid.Row="2" Grid.ColumnSpan="3" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" Margin="25,25,25,0">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="200*" MinWidth="158" MaxWidth="190" />
                <ColumnDefinition Width="700*" MaxWidth="600" />
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="2.95*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="2*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>

            <!--Name block-->
            <Label Grid.Row="0" Grid.Column="1" Style="{StaticResource LabelStyles}">Name :</Label>
            <TextBox Name="fName" Grid.Row="0" Grid.Column="2" Text="{Binding Path = PatientName}" Style="{StaticResource TextBoxStyle}"/>

            <!--Address block-->
            <Label Grid.Row="1" Grid.Column="1" Style="{StaticResource LabelStyles}" VerticalAlignment="Top"  Margin="0,7,15,0">Address :</Label>
            <StackPanel Grid.Row="1" Grid.Column="2" Orientation="Vertical" VerticalAlignment="Center"  Margin="0,2">
                <DockPanel LastChildFill="True" Margin="0,5">
                    <Label Style="{StaticResource AddressLabelStyles}" HorizontalAlignment="Right">Number :</Label>
                    <TextBox Name="Number" Text="{Binding Path =  PatientAddress.Number}" Style="{StaticResource TextBoxStyle}"/>
                </DockPanel>
                <DockPanel LastChildFill="True" Margin="0,5">
                    <Label Style="{StaticResource AddressLabelStyles}" Margin="12,0">Street :</Label>
                    <TextBox Name="Street" Text="{Binding Path =  PatientAddress.Street}" Style="{StaticResource TextBoxStyle}" Margin="3,0,0,0" />
                </DockPanel>
                <DockPanel LastChildFill="True" Margin="0,5">
                    <Label Style="{StaticResource AddressLabelStyles}" Margin="24,0">City :</Label>
                    <TextBox Name="City" Text="{Binding Path = PatientAddress.City}" Style="{StaticResource TextBoxStyle}" Margin="-9,0,0,0"/>
                </DockPanel>
            </StackPanel>

            <!--Gender block-->
            <Label  Grid.Row="2" Grid.Column="1" Style="{StaticResource LabelStyles}">Gender :</Label>
            <StackPanel Grid.Row="2" Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,6,0,0">
                <RadioButton Name="male" Command="{Binding Path=GetPatientGenderCommand}" CommandParameter="{Binding ElementName=male, Path=Content}" Content="Male" FontSize="14" Foreground="Black" Padding="5,-2.5" MinWidth="100"/>
                <RadioButton Name="female" Command="{Binding Path=GetPatientGenderCommand}" CommandParameter="{Binding ElementName=female, Path=Content}" Content="Female" FontSize="14" Foreground="Black" Padding="5,-2.5"/>
            </StackPanel>

            <!--Birthdate block-->
            <Label Grid.Row="3" Grid.Column="1" Style="{StaticResource LabelStyles}">Birthdate :</Label>
            <DatePicker Name="Date" Grid.Row="3" Grid.Column="2" SelectedDate ="{Binding Path = PatientDateOfBirth}" Style="{StaticResource TextBoxStyle}" MaxHeight="30" Padding="1"/>

            <!--Age block-->
            <Label Grid.Row="4" Grid.Column="1" Style="{StaticResource LabelStyles}">Age :</Label>
            <TextBox  Name="Age" Grid.Row="4" Grid.Column="2" Text="{Binding Path = PatientAge}" Style="{StaticResource TextBoxStyle}"/>

            <!--Image block-->
            <Label Grid.Row="5" Grid.Column="1" Style="{StaticResource LabelStyles}">Image :</Label>
            <DockPanel Grid.Row="5" Grid.Column="2" LastChildFill="True" VerticalAlignment="Center" MinHeight="30">
                <Button DockPanel.Dock="Left" Command="{Binding Path=GetPatientImageCommand, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource ButtonStyle}" Content="Browse" Margin="0,0,5,0"/>
                <TextBox Name="FileBrowser" DockPanel.Dock="Right" Text="{Binding Path= PatientImageSource}" Style="{StaticResource TextBoxStyle}" HorizontalAlignment="Stretch"/>
            </DockPanel>

            <!--Image view block-->
            <Image Name="ImageViewer" Grid.Row="6" Grid.Column="2" Source ="{Binding Path= PatientImageSource}" HorizontalAlignment="Left" MinWidth="95" MaxWidth="150" MinHeight="95" MaxHeight="150" Margin="0,0,0,10"/>

            <!--Department block-->
            <Label Grid.Row="7" Grid.Column="1" Style="{StaticResource LabelStyles}">Department :</Label>
            <ComboBox Name="Department" Grid.Row="7" Grid.Column="2" ItemsSource="{Binding Department}" SelectedItem="{Binding Path = PatientDepartment}" FontSize="14" Foreground="Black" BorderBrush="LightGray" BorderThickness="1" VerticalAlignment="Center" MinHeight="30"/>

            <!--Ward Block-->
            <Label Grid.Row="8" Grid.Column="1" Style="{StaticResource LabelStyles}">Ward :</Label>
            <ComboBox Name="Ward" Grid.Row="8" Grid.Column="2" ItemsSource="{Binding Ward}" SelectedItem="{Binding Path = PatientWard}" FontSize="14" Foreground="Black" BorderBrush="LightGray" BorderThickness="1" VerticalAlignment="Center" MinHeight="30"/>

            <!--Doctor Block-->
            <Label  Grid.Row="9" Grid.Column="1" Style="{StaticResource LabelStyles}">Doctor In Charge :</Label>
            <ComboBox Name="Doctor" Grid.Row="9" Grid.Column="2" ItemsSource="{Binding DocInCharge}" SelectedItem="{Binding Path = PatientDotorcInCharge}" FontSize="14" Foreground="Black" BorderBrush="LightGray" BorderThickness="1" VerticalAlignment="Center" MinHeight="30"/>
        </Grid>

        <!--Buttons Section-->
        <StackPanel Grid.Row="3" Grid.ColumnSpan="3" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="20,10,26,0" >
            <Button Command="{Binding Path=PreviewCommand}" Style="{StaticResource ButtonStyle}" Content="Print Preview" Margin="0,0,5,0"/>
            <Button Command="{Binding Path=ClearPatientCommand}" Style="{StaticResource ButtonStyle}" Content="Clear"/>
        </StackPanel>
    </Grid>
</UserControl>

这是我的 UserControl2 (PrintPreviewControl.xaml)
<UserControl x:Class="PatientRecordMVVM.Views.PrintPreviewControl"
             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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             FontFamily="{materialDesign:MaterialDesignFont}"
             xmlns:local="clr-namespace:PatientRecordMVVM.Views"
             mc:Ignorable="d" 
             d:DesignHeight="850" d:DesignWidth="800" Background="White">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="22*"/>
            <RowDefinition Height="2*"/>
        </Grid.RowDefinitions>

        <!--Main Sub Grid 1-->
        <Grid x:Name="MainSubGrid" Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="2.5*"/>
                <ColumnDefinition Width="3*"/>
                <ColumnDefinition Width="2.5*"/>
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height ="1.5*"/>
                <RowDefinition Height ="1.5*"/>
                <RowDefinition Height ="6.5*"/>
                <RowDefinition Height ="7*"/>
                <RowDefinition Height ="6*"/>
            </Grid.RowDefinitions>

            <Grid Grid.RowSpan="2" Grid.ColumnSpan="3" Background="#58af9d"/>

            <!--Patient Id block-->
            <StackPanel Grid.ColumnSpan="2" Style="{StaticResource StackpanelStyle}">
                <Label FontSize="16" FontWeight="Bold" Foreground="White">
                    Patient ID :
                </Label>
                <Label Content="{Binding PatientID}" FontSize="16" Foreground="White" Margin="5,0,0,0"/>
            </StackPanel>

            <!--Date block-->
            <StackPanel Grid.ColumnSpan="3" Style="{StaticResource StackpanelStyle}" HorizontalAlignment="Right">
                <Label FontSize="16" FontWeight="Bold" Foreground="White">
                    Date :
                </Label>
                <Label Name="date_time" Content="{Binding PatientRegisteredDate}" FontSize="16" Foreground="White" Margin="5,0"/>
            </StackPanel>

            <!--Title block-->
            <TextBlock Grid.Row="1" Grid.ColumnSpan="3" FontSize="20" FontWeight="Bold" Foreground="White" TextDecorations="Underline" HorizontalAlignment="Center" VerticalAlignment="Center">
                Patient Registration Information
            </TextBlock>

            <!--Sub grid 1-->
            <Grid Grid.Row="2" Grid.ColumnSpan="3" HorizontalAlignment="Center" VerticalAlignment="Stretch" Margin="25,10,25,20">

                <!--User's image View and name-->
                <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
                    <Ellipse Width="150" Height="150">
                        <Ellipse.Fill>
                            <ImageBrush x:Name="ImageViewer2" ImageSource="{Binding PatientImageSource}"/>
                        </Ellipse.Fill>
                    </Ellipse>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0,10">
                        <Label Style="{StaticResource LabelStyles}" Margin="0">Patient Name :</Label>
                        <TextBlock x:Name="ViewName" Text="{Binding PatientName}" Style="{StaticResource TextBlockStyle}"/>
                    </StackPanel>
                </StackPanel>
            </Grid>

            <!--Sub grid 2-->
            <GroupBox Grid.Row="3" Grid.ColumnSpan="3" HorizontalAlignment="Stretch"  VerticalAlignment="Center" Margin="25,-35,25,0">
                <GroupBox.Header>
                    <TextBlock Style="{StaticResource TextBlockStyleControl}">Patient Personal Information</TextBlock>
                </GroupBox.Header>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="500*"/>
                        <ColumnDefinition Width="500*"/>
                    </Grid.ColumnDefinitions>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="5*"/>
                        <RowDefinition Height="3*" MaxHeight="80"/>
                    </Grid.RowDefinitions>

                    <StackPanel Grid.Row="0" Grid.Column="0" Orientation="Vertical" Margin="10,5">
                        <Label Style="{StaticResource PreviewLabelStyles}" >Address :</Label>
                        <TextBlock x:Name="ViewANum" Text="{Binding PatientAddress.Number}" Style="{StaticResource TextBlockStyle}"/>
                        <TextBlock x:Name="ViewAStrt" Text="{Binding PatientAddress.Street}" Style="{StaticResource TextBlockStyle}"/>
                        <TextBlock x:Name="ViewACity" Text="{Binding PatientAddress.City}" Style="{StaticResource TextBlockStyle}"/>
                    </StackPanel>

                    <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Vertical" Margin="50,5,10,10">
                        <Label  Style="{StaticResource PreviewLabelStyles}">Gender :</Label>
                        <TextBlock x:Name="ViewGender" Text="{Binding PatientGender}" Style="{StaticResource TextBlockStyle}"/>
                    </StackPanel>

                    <StackPanel Grid.Row="1" Grid.Column="0" Orientation="Vertical" Margin="10,5">
                        <Label  Style="{StaticResource PreviewLabelStyles}">Date of Birth :</Label>
                        <TextBlock x:Name="ViewDob" Text="{Binding PatientDateOfBirth}" Style="{StaticResource TextBlockStyle}"/>
                    </StackPanel>

                    <StackPanel Grid.Row="1" Grid.Column="1" Orientation="Vertical" Margin="50,5,10,5">
                        <Label Style="{StaticResource PreviewLabelStyles}">Age :</Label>
                        <TextBlock x:Name="ViewAge" Text="{Binding PatientAge}" Style="{StaticResource TextBlockStyle}"/>
                    </StackPanel>
                </Grid>
            </GroupBox>

            <!--Sub grid 3-->
            <GroupBox Grid.Row="4" Grid.ColumnSpan="3" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="25,0,25,0">
                <GroupBox.Header>
                    <TextBlock Style="{StaticResource TextBlockStyleControl}">Patient Medical Information</TextBlock>
                </GroupBox.Header>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="500*"/>
                        <ColumnDefinition Width="500*"/>
                    </Grid.ColumnDefinitions>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="3*"/>
                        <RowDefinition Height="3*" />
                    </Grid.RowDefinitions>

                    <StackPanel Grid.Row="0" Grid.Column="0" Orientation="Vertical" Margin="10,5">
                        <Label Style="{StaticResource PreviewLabelStyles}">Department :</Label>
                        <TextBlock x:Name="ViewDepartment" Text="{Binding PatientDepartment}" Style="{StaticResource TextBlockStyle}"/>
                    </StackPanel>

                    <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Vertical" Margin="50,5,10,0">
                        <Label  Style="{StaticResource PreviewLabelStyles}">Ward :</Label>
                        <TextBlock x:Name="ViewWard" Text="{Binding PatientWard}" Style="{StaticResource TextBlockStyle}"/>
                    </StackPanel>

                    <StackPanel Grid.Row="1" Grid.Column="0" Orientation="Vertical" Margin="10,5,10,10">
                        <Label  Style="{StaticResource PreviewLabelStyles}">Doctor in Charge :</Label>
                        <TextBlock x:Name="ViewDoc" Text="{Binding PatientDotorcInCharge}" Style="{StaticResource TextBlockStyle}"/>
                    </StackPanel>
                </Grid>
            </GroupBox>
        </Grid>
        <StackPanel Grid.Row="2" Grid.ColumnSpan="3" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="20,10,26,0">
            <Button Command="{Binding Path=DefaultPrintCommand}" CommandParameter="{Binding ElementName=MainSubGrid}" Style="{StaticResource ButtonStyle}" Content="Default Print" Margin="0,0,5,0"/>
            <Button Command="{Binding Path=ConfigureAndPrintCommand}" CommandParameter="{Binding ElementName=MainSubGrid}" Style="{StaticResource ButtonStyle}" Content="Configure Print"/>
        </StackPanel>
    </Grid>
</UserControl>

这是 UserControl1 在其 ViewModel 中的按钮命令
    #region Handlers : Commands
    private void OnPreviewCommandExecute()
    {
        PatientRecordDetailsModel getPatientDetails = PopulatePatientDetails();

    }

我没有将整个代码放在 ViewModel 中,因为它太长了。

PatientRecordDetailsModel 是将发送到 UserControl2 的模型对象。

在 UserControl2 ViewModel 构造函数中,它将数据与 userControl View 绑定(bind)。
class PrintPreviewViewModel
    {
        #region Fields
        private IWindowService m_windowService;
        private PatientRecordDetailsModel patient;
        #endregion

        #region Constructors
        public PrintPreviewViewModel(PatientRecordDetailsModel patient)
        {
            this.patient = patient;

            PatientID = patient.PatientId;
            PatientRegisteredDate = patient.PatientRegisteredDate;
            PatientName = patient.PatientName;
            PatientAddress = patient.PatientAddress;
            PatientGender = patient.PatientGender;
            PatientDateOfBirth = patient.PatientDateOfBirth.ToShortDateString();
            PatientAge = patient.PatientAge;
            PatientImageSource = patient.PatientImageSource;
            PatientDepartment = patient.PatientDepartment;
            PatientWard = patient.PatientWard;
            PatientDotorcInCharge = patient.PatientDoctorInCharge;

            m_windowService = new WindowService();
        }
       }

在 HomeCareMain.xaml.cs 文件中,我定义了一个名为 的方法。 PrintPreviewButtonClicked() 将数据绑定(bind)到 UserControl2 的 DataContext 并使其可见。
public partial class HomeCareMain : Window
    {
        public HomeCareMain()
        {
            InitializeComponent();
            this.DataContext = new HomeCareMainViewModel();
            PatientRecordDetailsViewModel patientRecordDetailsViewModel = new PatientRecordDetailsViewModel();           
            AddPatient.DataContext = patientRecordDetailsViewModel;
            AddPatient.Visibility = Visibility.Hidden;
            PrintPreview.Visibility = Visibility.Hidden;
        }

        public void PrintPreviewButtonClicked(PatientRecordDetailsModel patient)
        {
            PrintPreview.DataContext = new PrintPreviewViewModel(patient);
            PrintPreview.Visibility = Visibility.Visible;
        }
     }

问题是我想发送 PatientRecordDetailsModel 在不违反 MVVM 架构的情况下反对 HomeCareMain.xaml.cs。

或者有没有其他方法可以做到这一点。

我为用户控件和主窗口使用了单独的 View 模型,因为它们有自己的职责要执行。

我真的希望你能帮我解决这个问题。

最佳答案

您应该从两个 UserControls 绑定(bind)到相同的 View 模型。 ,或使用事件聚合器或信使以松散耦合的方式将事件或消息从一个组件发送到另一个组件,如 this 中所述博客文章。

如果您不使用框架,则必须自己实现事件聚合器。

关于c# - 如何从UserControl1 WPF MVVM中的按钮将数据从一个UserControl1传递到UserControl2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61453553/

相关文章:

wpf - 将 ContextMenu 的来源传递给 WPF 命令

c# - 将 WPF Combobox 的 SelectedItem 转换为 Color 会导致异常

c# - PetaPoco 存储过程错误 "Incorrect syntax near the keyword ' FROM'。"}

C# for循环显示所有记录

wpf - Windows 8 XAML 多列文本

.net - 在运行时获取XAML类型的URI

c# - 在 ScrollViewer wpf 中更改 ScrollBar 的背景颜色

c# - .SendMailAsync() 在 MVC 中的使用

c# - 将文件写入连接到 Windows PC 的 PNP 设备

c# - 在文件中查找文本并检索行号