MVVM Model doubts

asked 2021-11-01T18:10:54.703+00:00
Ajeje Brazorf 101 Reputation points

I'm a mvvm beginner, I'm trying to port an old app from winform. basically it have 3 docked usercontrol where a log is displayed in as a 3d chart in one control, as 2d chart pie in the second and as a list of points in the last. When the user start the app an openfiledialog appear to load the binary log file, that are loaded into an array.
Now moving in mvvm, I managed to use a dock control and work fine;
1- I'm struggle to understand how to load the file trough the openfiledialog from the shell, and made it available for the 3 viewmodels (of the usercontrols).
2-I need also to save some user setting like background color, etc. In winform I used mysetting, and worked fine. Now using WPF .NET 5.0, seem to be deprecated right? Is there a simple alternative, easy to implement without mess up the mvvm that I'm slowly building?
I'm targeting .NET 5.0, and using Sylet MVVM Framework(it's similar to CaliburnMicro).
Any advice?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,169 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
1,823 questions
No comments
{count} votes

Accepted answer
  1. answered 2021-11-03T06:19:55.72+00:00
    Peter Fleischer (Freelancer) 17,386 Reputation points

    Hi Ajeje,
    try following demo with 2 UserControls and 3 ViewModels; ViewModel0 - main ViewModel, ViewModel1 - for UserControl1, ViewModel2 - for UserControl2.

    XAML Main Window:

    <UserControl x:Class="Window096UC2"  
                 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:WpfControlLibrary1.WpfApp096"  
                 mc:Ignorable="d"   
                 d:DesignHeight="450" d:DesignWidth="800">  
      <UserControl.Resources>  
        <local:ViewModel2 x:Key="vm2"/>  
      </UserControl.Resources>  
      <Border BorderBrush="Green" BorderThickness="2">  
        <StackPanel>  
          <Label Content="UserControl 2"/>  
          <TextBox Text="{Binding Text0, UpdateSourceTrigger=PropertyChanged}"  
                   Margin="5"/>  
          <TextBox Text="{Binding Text2, Source={StaticResource vm2}, UpdateSourceTrigger=PropertyChanged}"  
                   Margin="5"/>  
          <TextBox Text="{Binding Text2, Source={StaticResource vm2}, UpdateSourceTrigger=PropertyChanged}"  
                   Margin="5"/>  
        </StackPanel>  
      </Border>  
    </UserControl>  
    

    XAML UserControl1:

    <UserControl x:Class="Window096UC1"  
                 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:WpfControlLibrary1.WpfApp096"  
                 mc:Ignorable="d"   
                 d:DesignHeight="450" d:DesignWidth="800">  
      <UserControl.Resources>  
        <local:ViewModel1 x:Key="vm1"/>  
      </UserControl.Resources>  
      <Border BorderBrush="Red" BorderThickness="2">  
        <StackPanel>  
          <Label Content="UserControl 1"/>  
          <TextBox Text="{Binding Text0, UpdateSourceTrigger=PropertyChanged}"  
                   Margin="5"/>  
          <TextBox Text="{Binding Text1, Source={StaticResource vm1}, UpdateSourceTrigger=PropertyChanged}"  
                   Margin="5"/>  
          <TextBox Text="{Binding Text1, Source={StaticResource vm1}, UpdateSourceTrigger=PropertyChanged}"  
                   Margin="5"/>  
        </StackPanel>  
      </Border>  
    </UserControl>  
    

    XAML UserControl2:

    <UserControl x:Class="Window096UC2"  
                 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:WpfControlLibrary1.WpfApp096"  
                 mc:Ignorable="d"   
                 d:DesignHeight="450" d:DesignWidth="800">  
      <UserControl.Resources>  
        <local:ViewModel2 x:Key="vm2"/>  
      </UserControl.Resources>  
      <Border BorderBrush="Green" BorderThickness="2">  
        <StackPanel>  
          <Label Content="UserControl 2"/>  
          <TextBox Text="{Binding Text0, UpdateSourceTrigger=PropertyChanged}"  
                   Margin="5"/>  
          <TextBox Text="{Binding Text2, Source={StaticResource vm2}, UpdateSourceTrigger=PropertyChanged}"  
                   Margin="5"/>  
          <TextBox Text="{Binding Text2, Source={StaticResource vm2}, UpdateSourceTrigger=PropertyChanged}"  
                   Margin="5"/>  
        </StackPanel>  
      </Border>  
    </UserControl>  
    

    ViewModels:

    Imports System.ComponentModel  
    Imports System.Runtime.CompilerServices  
      
    Namespace WpfApp096  
      
      Public Class ViewModel0  
        Inherits ViewModelBase  
        Private _text0 As String  
        Public Property Text0 As String  
          Get  
            Return Me._text0  
          End Get  
          Set(value As String)  
            Me._text0 = value  
            OnPropertyChanged()  
          End Set  
        End Property  
      End Class  
      
      Public Class ViewModel1  
        Inherits ViewModelBase  
        Private _text1 As String  
        Public Property Text1 As String  
          Get  
            Return Me._text1  
          End Get  
          Set(value As String)  
            Me._text1 = value  
            OnPropertyChanged()  
          End Set  
        End Property  
      End Class  
      
      Public Class ViewModel2  
        Inherits ViewModelBase  
        Private _text2 As String  
        Public Property Text2 As String  
          Get  
            Return Me._text2  
          End Get  
          Set(value As String)  
            Me._text2 = value  
            OnPropertyChanged()  
          End Set  
        End Property  
      End Class  
      
      Public Class ViewModelBase  
          Implements INotifyPropertyChanged  
          Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged  
        Protected Sub OnPropertyChanged(<CallerMemberName> Optional propName As String = "")  
          RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))  
        End Sub  
      End Class  
      
    End Namespace  
    

    Result:

    145910-x.gif

    No comments

1 additional answer

Sort by: Most helpful
  1. answered 2021-11-02T05:14:04.977+00:00
    Peter Fleischer (Freelancer) 17,386 Reputation points

    Hi Ajeje,

    1- I'm struggle to understand how to load the file trough the openfiledialog from the shell, and made it available for the 3 viewmodels (of the usercontrols).

    You can use the same ViewModel instance in each UserControl.

    2-I need also to save some user setting like background color, etc. In winform I used mysetting, and worked fine. Now using WPF .NET 5.0, seem to be deprecated right?

    You write your own Setting class. For properties in this setting class you can use static members. To save settings you can use serializing.