The right way to share object between ViewModels

hed bisker

Member
Joined
Aug 5, 2018
Messages
16
Programming Experience
1-3
What I did it I have Class1

Class1:
 public class Class1
    {
        public Class1(string x)
        {
            X = x;
        }
        public string X { get; set; }
    }


I have the main Window

main window:
<Window x:Class="WpfApp2.MainWindow"
        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:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>

        <StackPanel>
            <TextBox Text="{Binding C1.X, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>

            <ListBox Margin="5" Name="EventList"
                         ItemsSource="{Binding SensorEventList,UpdateSourceTrigger=PropertyChanged,IsAsync=True}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <ContentControl Content="{Binding}" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
    </Grid>
</Window>

and code-behind :

main window code behind:
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainWindowViewModel();
        }
    }


It has his ViewModel I create class1 object and pass it to UserControleSub constructor its the second window

Main View Model:
public class MainWindowViewModel
    {
        public Class1 C1 { get; set; } = new Class1("123");

        public MainWindowViewModel()
        {
            SensorEventList.Add(new UserControleSub(C1));

        }

        public ObservableCollection <UserControleSub> SensorEventList { get; set; } = new ObservableCollection<UserControleSub>();
    }

Then I pass it to the second window ViewModel constructor The code behind

second window ViewModel constructor:
 public partial class UserControleSub : UserControl
    {
        public UserControleSub(Class1 c1)
        {
            InitializeComponent();
            DataContext = new UserControleSubViewModel(c1);
        }
    }

and the second window ViewModel looks like that:

second window ViewModel:
 public class UserControleSubViewModel
    {
        public UserControleSubViewModel(Class1 c1)
        {
            C1 = c1;
        }
        public Class1 C1 { get; set; }
    }


and its XAML file :

second window XAML:
<UserControl x:Class="WpfApp2.UserControleSub"
        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:WpfApp2"
        mc:Ignorable="d" Height="450" Width="800">
    <Grid>
        <TextBlock Text="{Binding C1.X,UpdateSourceTrigger=PropertyChanged}" FontSize="30"></TextBlock>
    </Grid>
</UserControl>


The strategy is to pass it by the window constructor and to the ViewModel constructor,
it is a Good Idea? it makes the ViewModel depend on the window and the second Window depend on the first window in a bad way right?
There is any better way to do it?
 
Back
Top Bottom