question

AjejeBrazorf-0832 avatar image
0 Votes"
AjejeBrazorf-0832 asked PeterFleischer-3316 answered

MVVM Model doubts

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-wpfdotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered

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



x.gif (94.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PeterFleischer-3316 avatar image
1 Vote"
PeterFleischer-3316 answered AjejeBrazorf-0832 commented

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.



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks, can you show a little example(for 1) please?

0 Votes 0 ·