Home > OS >  C# WPF How i can pass data from User Control to Main VM
C# WPF How i can pass data from User Control to Main VM

Time:12-16

I'm trying to write an application for accounting for employees, orders, departments.

I have an element editor window, in which, depending on the selected element (Employee/Order/Department), the required User Control is substituted

Faced the problem of passing data from the View Model User Control to the View Model of the parent window.

How can I pass SelectedEmployee from EmployeeEditViewModel to CreateEditViewModel?

ParentWindow CreateEditViewModel

public class CreateEditWindowViewModel : BaseViewModel
{
    private BaseViewModel selectedViewModel;
    private object selectedItem;
    private IMessenger messenger;

    // Repositories 
    private IRepository<Employee> employeeRepository;
    private IRepository<Order> orderRepository;
    private IRepository<Subdivision> subdivisionRepository;

    // Commands
    public ICommand CloseWindowCommand { get; private set; }
    public ICommand MaximizeWindowCommand { get; private set; }
    public ICommand MinimizeWindowCommand { get; private set; }
    public ControllCommand SaveItemCommand { get; private set; }

    public CreateEditWindowViewModel(IMessenger messenger)
    {
        employeeRepository = new EmployeeRepository();
        subdivisionRepository = new SubdivisionRepository();
        orderRepository = new OrderRepository();

        this.messenger = messenger;

        messenger.Subscribe<RequestIdMessage>(this, GetId);
        messenger.Subscribe<EditItemMessage>(this, GetItem);

        CloseWindowCommand = new CloseWindowCommand(this);
        MaximizeWindowCommand = new MaximizeWindowCommand(this);
        MinimizeWindowCommand = new MinimizeWIndowCommand(this);

        SaveItemCommand = new ControllCommand(SaveItem);
        windowId = -1;
    }

xaml CreateEditView

<Window x:Class="MerryWaterCarrier.View.CreateEditWindow"
    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:local="clr-namespace:MerryWaterCarrier.View"
    xmlns:viewedit="clr-namespace:MerryWaterCarrier.View.EditorWindow"
    DataContext="{Binding Path=CreateEditWindowViewModel, Source={StaticResource ViewModelLocator}}"
    mc:Ignorable="d"
    Title="CreateEditWindow" Height="450" Width="800"
    Background="#36363F"
    WindowStyle="None"
    WindowStartupLocation="CenterScreen">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="30*"/>
        <ColumnDefinition Width="1*"/>
    </Grid.ColumnDefinitions>

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

    <Border Margin="5,0,5,0" Background="#252525" Grid.Row="0" Grid.Column="1">
        <StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
            <Button Command="{Binding MinimizeWindowCommand}"
                    Width="20" Height="20"
                    Content="           
  • Related