C
C#6mo ago
Core Dream

Dependency Injection + MVVM?

I followed a Tim Corey tutorial on Dependency Injection in WPF but he did not use MVVM just raw code behind. How hard would be to switch over to MVVM? I just want an app where I have a MainView, SettingsView, and a few other Views. He uses Interfaces, etc and I'm lost. :< What should I do?
247 Replies
Core Dream
Core DreamOP6mo ago
I hate confusion, bad enough MVVM can be confusing sometimes for me lol
lycian
lycian6mo ago
IMO switching to MVVM just for the sake of it on a small app would only be useful to learn. That said, if you already have DI, it shouldn't be that hard to do
Core Dream
Core DreamOP6mo ago
i know mvvm but i dunno how to impliment tim corey's di as it's bad practice what he did apparently :/ maybe i will just do the MVVM part and post my code later if someone wants to help me with the DI part XD i cant fiddle with unknowns when i need to make something fast its why i keep going to winforms, it just works :x
Core Dream
Core DreamOP6mo ago
IAmTimCorey
YouTube
Dependency Injection in WPF in .NET 6 Including the Factory Pattern
.NET Core has a built-in dependency injection that we use in every web project type we create. However, did you know that you can also use that same dependency injection in the .NET Core version of WPF? In this video, we are going to enable dependency injection in WPF and then use it for some common scenarios, including creating multiple forms. ...
Core Dream
Core DreamOP6mo ago
i used his DI setup but its for non-mvvm i know some mvvm but iam not good at combining ideas into 1, autism sucks 😦 so i always get stuck with concepts
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
I added the common Models, ViewModels, Views folder added CommunityToolkit.MVVM as well etc so i got that far 😄
leowest
leowest6mo ago
push to github and post link 😉
Core Dream
Core DreamOP6mo ago
wheee i gotta make a new ssh, sec pushing https://github.com/coredreamstudios/TitanSTUDIO yay 😄 ignore the MainView.xaml window, i renamed the original to MainViewDI.xaml so i can work on my UI while i wait
leowest
leowest6mo ago
u forgot to set it pub
Core Dream
Core DreamOP6mo ago
i cant keep it private? sec
leowest
leowest6mo ago
if u keep it private then u have to invite people in order for them to see it
Core Dream
Core DreamOP6mo ago
done
leowest
leowest6mo ago
leowest77
Core Dream
Core DreamOP6mo ago
its now public
lycian
lycian6mo ago
In the toolkit MVVM samples they have basically what you're looking for https://github.com/CommunityToolkit/MVVM-Samples/blob/master/samples/MvvmSampleUwp/App.xaml.cs#L56
GitHub
MVVM-Samples/samples/MvvmSampleUwp/App.xaml.cs at master · Communit...
Sample repo for MVVM package. Contribute to CommunityToolkit/MVVM-Samples development by creating an account on GitHub.
lycian
lycian6mo ago
They add the viewmodels as transient in the container, the views can then either: be added to the container, or get the view model from the container it uses UWP but the same applies for WPF
leowest
leowest6mo ago
@Core Dream Studios a lot of the things u have in your viewdi, will go in the viewmodel or are the 2 different windows?
Core Dream
Core DreamOP6mo ago
minus the _click's of course as we will use ICommand's right?
leowest
leowest6mo ago
some of the ui events would become commands yes
Core Dream
Core DreamOP6mo ago
not sure why tim made child forms like that
leowest
leowest6mo ago
but imagine for example if u wanted to load something when the window loads
Core Dream
Core DreamOP6mo ago
you cant customize em as they're all the same XD
leowest
leowest6mo ago
then what we often do is we wire it from the view to the vm
Core Dream
Core DreamOP6mo ago
yep, that i understand do i even need factories? i dont even know wth those even are XD i only did what he did
leowest
leowest6mo ago
u may use factories in some scenarios but for a simple app at the level u are right now u shouldn't have to worry about it all u have are either singleton or scoped stuff
Core Dream
Core DreamOP6mo ago
my app is very simple, all it will do is write/read files, and ram addresses, nothing special no web, etc
leowest
leowest6mo ago
its worth learning about the lifecycle of things so u understand which one u should use
Core Dream
Core DreamOP6mo ago
singleton i know means a window/form which you use once, aka the main one that i get the transient means more than 1 use according to what tim said
leowest
leowest6mo ago
singleton will live trhu the entire duration of the app
Core Dream
Core DreamOP6mo ago
im gonna try to update my code to reflect the DI with MVVM then push changes to github yes, aka main window 🙂
leowest
leowest6mo ago
also u added CTK but you're not using it u still have INPC stuff
Core Dream
Core DreamOP6mo ago
CTK?
leowest
leowest6mo ago
CommunityToolkit abbrev
Core Dream
Core DreamOP6mo ago
yeah i didnt switch over yet was doing some raw basic stuff for now but gonna switch in a minute i will update my github in 20min
leowest
leowest6mo ago
its ok im just going thru what is in there for now and seeing what changes are required to plug mvvm with di
Core Dream
Core DreamOP6mo ago
that code behind Tim did with the Main .xaml, thats bad practice for mvvm for sure
leowest
leowest6mo ago
yes because it mix view with things that aren't view
Core Dream
Core DreamOP6mo ago
i think datacontext would be best here
leowest
leowest6mo ago
DataContext would still live in the view althou u can also set it via di initialization
leowest
leowest6mo ago
the concept of DI is
No description
leowest
leowest6mo ago
you have an object with a dependency when that object is initialize DI will query its setvices and see if it have an instance it can fullfil that dependency so it looks up oh I have a singleton MainViewModel, let me pass it via that constructor
Core Dream
Core DreamOP6mo ago
i will see how far i can get now, will update github when done 🙂
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Windows;
using TitanSTUDIO.Views;
using SampleWpfLibrary;
using TitanSTUDIO.StartupHelpers;

namespace TitanSTUDIO;

public partial class App : Application
{
public static IHost? AppHost { get; private set; }

public App()
{
AppHost = Host.CreateDefaultBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddSingleton<MainViewDI>();
services.AddViewFactory<ChildView>();
services.AddTransient<IDataAccess, DataAccess>();
})
.Build();
}

protected override async void OnStartup(StartupEventArgs e)
{
await AppHost!.StartAsync();

var startupForm = AppHost.Services.GetRequiredService<MainViewDI>();
startupForm.Show();

base.OnStartup(e);
}

protected override async void OnExit(ExitEventArgs e)
{
await AppHost!.StopAsync();

base.OnExit(e);
}
}
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Windows;
using TitanSTUDIO.Views;
using SampleWpfLibrary;
using TitanSTUDIO.StartupHelpers;

namespace TitanSTUDIO;

public partial class App : Application
{
public static IHost? AppHost { get; private set; }

public App()
{
AppHost = Host.CreateDefaultBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddSingleton<MainViewDI>();
services.AddViewFactory<ChildView>();
services.AddTransient<IDataAccess, DataAccess>();
})
.Build();
}

protected override async void OnStartup(StartupEventArgs e)
{
await AppHost!.StartAsync();

var startupForm = AppHost.Services.GetRequiredService<MainViewDI>();
startupForm.Show();

base.OnStartup(e);
}

protected override async void OnExit(ExitEventArgs e)
{
await AppHost!.StopAsync();

base.OnExit(e);
}
}
i guess i iwll delete this as its not mvvm
leowest
leowest6mo ago
that is
Core Dream
Core DreamOP6mo ago
oh
leowest
leowest6mo ago
part of it at least I will update u in a bit
Core Dream
Core DreamOP6mo ago
ok
leowest
leowest6mo ago
No description
Core Dream
Core DreamOP6mo ago
i dont understand why he did child view/forms like he did since you cant modify them individually XD like ui etc
leowest
leowest6mo ago
well I did not watch his video he must have a reason but it all depends how the childs will be used
Core Dream
Core DreamOP6mo ago
but i dont want a cookie cutter child form each window will have its own look tbh for me aka SettingsView
leowest
leowest6mo ago
No description
leowest
leowest6mo ago
also u realize button have a control called Content where u can write Button 2 instead of wrapping a textblock right?
Core Dream
Core DreamOP6mo ago
oh, yeah , i know, im doing it in a rush so i can focus on the mvvm/di first
leowest
leowest6mo ago
well normally wpf is not designed to have multiple windows like winforms u can
Core Dream
Core DreamOP6mo ago
i also need to do templating which is not needed right now
leowest
leowest6mo ago
but usually you have a navigation using usercontrols that swap to different locations
Core Dream
Core DreamOP6mo ago
so wouldnt i use pages then?
leowest
leowest6mo ago
with that one window u have
Core Dream
Core DreamOP6mo ago
err user controls
leowest
leowest6mo ago
UserControls is fine I dont ever recall ever using Pages with WPF maybe once to explain it to some one about the navigation system they have in the docs but that was it
Core Dream
Core DreamOP6mo ago
pages is more of a ASP.NET / Blazor thing im sure but yes i can do Main and UC and use the child style he did welp i got a lot to work on before i can upload a updated code base so probably wont be for a hour or more XD now with user controls those also will need viewmodels right? based on what they will do each SettingsUserControlViewModel etc?
leowest
leowest6mo ago
some will yes u dont need to append UserControl to the name
Core Dream
Core DreamOP6mo ago
ok
leowest
leowest6mo ago
just SettingView/ViewModel
Core Dream
Core DreamOP6mo ago
ty I will update here when i got something useful to show thank you @leowest 🙂
leowest
leowest6mo ago
np another thing is that ur not using binding at all
Core Dream
Core DreamOP6mo ago
yeah i know
leowest
leowest6mo ago
I assume because u were copying Tim's stuff
Core Dream
Core DreamOP6mo ago
i was doing raw im switchingt to mvvm now yes 🙂 MVVM = ❤️
leowest
leowest6mo ago
leowest
leowest6mo ago
so ChildView becomes UserControl from Window via xaml I added a 3rd grid where I have a ControlControl to show the childview inside
<ContentControl Grid.Row="1" Content="{Binding Child}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type vm:ChildViewModel}">
<local:ChildView />
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
<ContentControl Grid.Row="1" Content="{Binding Child}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type vm:ChildViewModel}">
<local:ChildView />
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
Core Dream
Core DreamOP6mo ago
ControlControl? oh
leowest
leowest6mo ago
the datatemplate defines the vm for the childview do I dont need to wire anything on the View of Child DI will do it all by it self
Core Dream
Core DreamOP6mo ago
the buttons werent meant to be on top tbh i was gonna do something gamified
leowest
leowest6mo ago
it doesn't matter its just to show how it all wires nad works its fine
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
oh ok
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SampleWpfLibrary;
using System.Windows;
using TitanSTUDIO.ViewModels;
using TitanSTUDIO.Views;

namespace TitanSTUDIO;

public partial class App : Application
{
public static IHost? AppHost { get; private set; }

public App()
{
AppHost = Host.CreateDefaultBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddTransient<IDataAccess, DataAccess>();
// services.AddScoped<ChildViewModel>();
// services.AddScoped<ChildView>();
services.AddSingleton<MainViewModel>();
services.AddSingleton<MainView>();
}).Build();
}
}
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SampleWpfLibrary;
using System.Windows;
using TitanSTUDIO.ViewModels;
using TitanSTUDIO.Views;

namespace TitanSTUDIO;

public partial class App : Application
{
public static IHost? AppHost { get; private set; }

public App()
{
AppHost = Host.CreateDefaultBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddTransient<IDataAccess, DataAccess>();
// services.AddScoped<ChildViewModel>();
// services.AddScoped<ChildView>();
services.AddSingleton<MainViewModel>();
services.AddSingleton<MainView>();
}).Build();
}
}
i should of kept the overrides? i removed them (not focused on child atm its why I commented it out)
leowest
leowest6mo ago
its ok u do u im just showing what u have wired what it would could look like
Core Dream
Core DreamOP6mo ago
well now my window wont even load
leowest
leowest6mo ago
so that contentcontrol I showed u above I can easily add more templates to it of different childs
Core Dream
Core DreamOP6mo ago
so i probably will need the overrides on OnStartup etcc
leowest
leowest6mo ago
and swap it via the vm
Core Dream
Core DreamOP6mo ago
thats fair
leowest
leowest6mo ago
No description
Core Dream
Core DreamOP6mo ago
<Application x:Class="TitanSTUDIO.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TitanSTUDIO">
<Application.Resources>

</Application.Resources>
</Application>
<Application x:Class="TitanSTUDIO.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TitanSTUDIO">
<Application.Resources>

</Application.Resources>
</Application>
leowest
leowest6mo ago
Child is what is bound to the contentcontrol
Core Dream
Core DreamOP6mo ago
i removed the startupwindow as tim said to do
leowest
leowest6mo ago
yes u have to since u wire it from the app.xaml.cs
Core Dream
Core DreamOP6mo ago
ok but how to i load it now? i removed the OnStartup/OnExit oh ok, so same way got it so many variables 😂
leowest
leowest6mo ago
yeah what is in the app.xaml.cs is basic setup for the di it doesnt really change much
Core Dream
Core DreamOP6mo ago
im gonna make a base template out of this project im sure i can save it as a Project Template yay
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
<StatusBar Grid.Row="1" Background="#222" Foreground="WhiteSmoke"> <StatusBarItem Margin="20 5" Content="{Binding Version}"/> <StatusBarItem Margin="20 5" Content="Item 2"/> <StatusBarItem Margin="20 5" Content="Item 3"/> </StatusBar> and bind works 🙂 I think I'm getting somewhere ( I didn't do templating, its why you see margin repeating 😄 ) So... Interfaces, is basically like how C++ has header files? you have the main class then a interface which refers to the methods? I will just read the msdn docs on those
leowest
leowest6mo ago
no interfaces are like contracts c++ often split the signature in a header file and the method in the cpp file i.e.: signature
private void GetData();
private void GetData();
leowest
leowest6mo ago
to illustrate what I was saying earlier
leowest
leowest6mo ago
<ContentControl Grid.Row="1" Content="{Binding Child}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type vm:ChildViewModel}">
<local:ChildView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:Child2ViewModel}">
<local:ChildView2 />
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
<ContentControl Grid.Row="1" Content="{Binding Child}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type vm:ChildViewModel}">
<local:ChildView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:Child2ViewModel}">
<local:ChildView2 />
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
I can easily swap between child a and b where button 1 is accessing your data
Core Dream
Core DreamOP6mo ago
as long as each child, has its own uI since im gonna many, one for settings with comboboxes, buttons, textboxes etc. and others with similar but for other roles
leowest
leowest6mo ago
if you have a UserControl you could just add it to your xaml using say
<local:SomeUserControl />
<local:SomeUserControl />
this would make so it inherits the datacontext from the parent, so if the parent was MainViewModel, it inherits it. however if u define a DataTemplate with a specific ViewModel it will be given that
Core Dream
Core DreamOP6mo ago
ok
leowest
leowest6mo ago
the datatemplate is also what allows u to use the BaseViewModel as a type to render the View instead of having to use the View and wire everything
Core Dream
Core DreamOP6mo ago
Getting overwhelmed but trying
leowest
leowest6mo ago
well I will just leave u to it once u finish doing your thing and update let me know and I will take a look again
Core Dream
Core DreamOP6mo ago
thank you, def getting somewhere 🙂 almost got it going XD i can't get this to work with UserControls so far
leowest
leowest6mo ago
its ok do the rest first get the rest working and then u can go into that
Core Dream
Core DreamOP6mo ago
oh, was trying to load up the usercontrol i made XD hrm
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
my code didn't update on the github XD im getting sloppy 😦
leowest
leowest6mo ago
did u forget to set the upstream or something check what origin/master are set to
Core Dream
Core DreamOP6mo ago
its not letting me push i think even though i told it too if i switch to origin/master on remote and try i get this
Core Dream
Core DreamOP6mo ago
No description
leowest
leowest6mo ago
did u commit and then push also that is not what I meant by check master/origin check what its set to sometimes its set to local so it may not be pushing to github
Core Dream
Core DreamOP6mo ago
No description
leowest
leowest6mo ago
that doesnt tell u what master is set to
Core Dream
Core DreamOP6mo ago
where am i not looking?
leowest
leowest6mo ago
git remote show origin for example
Core Dream
Core DreamOP6mo ago
ok F:\Backups\Backup-DevDrive\source\csharp\wpf\TitanSTUDIO>git remote show origin 'git' is not recognized as an internal or external command, operable program or batch file. ugh im getting stupid, timeout for tonight i guess
leowest
leowest6mo ago
:catlaugh:
Core Dream
Core DreamOP6mo ago
i will install git
leowest
leowest6mo ago
looks like u dont have git installed outside the dev env or something
Core Dream
Core DreamOP6mo ago
yeah, i hardly used it til now, i will install
leowest
leowest6mo ago
sometimes its easier to get the info via cli
Core Dream
Core DreamOP6mo ago
No description
leowest
leowest6mo ago
u have a typo ah nvm u fixed after git push origin master:master
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
no change
leowest
leowest6mo ago
yeah I dunno it looks fine visually maybe zip it to save the changes and try to git push origin I assume those changes u showed in the window were commited right git commit -m "Additional changes" if they were this could should say there is no changes or w/e
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
let me do this, brb
leowest
leowest6mo ago
ah u didnt set that up on visual studio
Core Dream
Core DreamOP6mo ago
not surprised 😂 we good now
Core Dream
Core DreamOP6mo ago
GitHub
TitanSTUDIO/TitanSTUDIO at master · coredreamstudios/TitanSTUDIO
Contribute to coredreamstudios/TitanSTUDIO development by creating an account on GitHub.
leowest
leowest6mo ago
u dont need to do
private string _version = "1.00";
public string Version
{
get => _version;
set => SetProperty(ref _version, value);
}
private string _version = "1.00";
public string Version
{
get => _version;
set => SetProperty(ref _version, value);
}
Core Dream
Core DreamOP6mo ago
that was just for testing
leowest
leowest6mo ago
u can just do
[ObservableProperty]
private string _version = "1.00";
[ObservableProperty]
private string _version = "1.00";
less boilerplate
Core Dream
Core DreamOP6mo ago
ah
leowest
leowest6mo ago
it generates all the rest for u
Core Dream
Core DreamOP6mo ago
i got the old fashioned way stuck in my head XD isnt human memory fun? 😊
leowest
leowest6mo ago
No description
leowest
leowest6mo ago
this is a view
Core Dream
Core DreamOP6mo ago
but its a UC? confusing
leowest
leowest6mo ago
yes it is confusing but you're using that one as a view
Core Dream
Core DreamOP6mo ago
i want it to be a uc so its in the mainview window
leowest
leowest6mo ago
when you're create a UserControl for the purpose of it being just a control that is a different thing
sibber
sibber6mo ago
user controls are views
leowest
leowest6mo ago
it will be a child in the mainwindow for settings where u want its own vm
sibber
sibber6mo ago
uis are called views
Core Dream
Core DreamOP6mo ago
see, i hate layout 😦
leowest
leowest6mo ago
just think of it as Features
Core Dream
Core DreamOP6mo ago
i dunno if i want settings as a self contained uc or a window
leowest
leowest6mo ago
forget View and ViewModel imagine this
Core Dream
Core DreamOP6mo ago
maybe im using UserControls for the wrong reasons as UC is to make CUSTOM control i get it now
leowest
leowest6mo ago
root
- Features
• Settings
• Home
• Login
- LoginView
- LoginViewModel
- LoginModel
- Services
app.xaml
app.xaml.cs
root
- Features
• Settings
• Home
• Login
- LoginView
- LoginViewModel
- LoginModel
- Services
app.xaml
app.xaml.cs
Core Dream
Core DreamOP6mo ago
so i must use a window as a view 🙂
leowest
leowest6mo ago
no
Core Dream
Core DreamOP6mo ago
nice tree
leowest
leowest6mo ago
window is always the shell
Core Dream
Core DreamOP6mo ago
ok
leowest
leowest6mo ago
its the top level where u can house views
Core Dream
Core DreamOP6mo ago
ok so MainView.xaml is fine
leowest
leowest6mo ago
I usually name it Shell to make it less complciated but sure u can name it MainWindowView MainView it all makes sense
Core Dream
Core DreamOP6mo ago
some people make different ways of doing things
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
example, this app, also WPF using popup windows so its confusing 😦
leowest
leowest6mo ago
u can have Dialog Popup as a window that is no problem
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
what would this be called
leowest
leowest6mo ago
you would have a dialogservice that would summon that window etc
Core Dream
Core DreamOP6mo ago
i am horrible with design, its not something im good at
leowest
leowest6mo ago
honestly that would be similar a toolwindow
Core Dream
Core DreamOP6mo ago
i can ironically make logos, illustrations etc but not ui
leowest
leowest6mo ago
these are just window/view concepts but have u stopped to think u may not new a separted window? maybe your window is alredy big enough that simple having a navigation with in it is enough to house the settings page Honestly I think of the whole layout like this Shell/Window/Main as the casket all other UserControls are Views that are housed by it or summoned by it in some fashion
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
ok so i got rid of the UserControls folder and made a Settings Window named SettingsView if i was rich id just hire someone to do the ui but im on ssi trying to make a life for myself and failing (perm physical disability) im very close too :X
leowest
leowest6mo ago
so you're set that ur settingsview must be a window on its own then u will need to write a service that u will inject to your vm that needs to call it that will open/close/manage it
Core Dream
Core DreamOP6mo ago
well it dont need to be its own window it could be embedded
leowest
leowest6mo ago
well u create it as Window
Core Dream
Core DreamOP6mo ago
hell, do it as a tab
leowest
leowest6mo ago
so it can only be a window
Core Dream
Core DreamOP6mo ago
so what do I use? back to usercontrols?
leowest
leowest6mo ago
if u want it to be housed inside a window it needs to be a UserControl
Core Dream
Core DreamOP6mo ago
ok got it
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
ok there makes sense since embedded wont have its own modal buttons and title
leowest
leowest6mo ago
yep now u make a vm for it and wire it all in the di and datatemplate and that's it
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
all set
leowest
leowest6mo ago
u dont need that u just need the datatemplate
MODiX
MODiX6mo ago
leowest
<ContentControl Grid.Row="1" Content="{Binding Child}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type vm:ChildViewModel}">
<local:ChildView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:Child2ViewModel}">
<local:ChildView2 />
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
<ContentControl Grid.Row="1" Content="{Binding Child}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type vm:ChildViewModel}">
<local:ChildView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:Child2ViewModel}">
<local:ChildView2 />
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
Quoted by
React with ❌ to remove this embed.
Core Dream
Core DreamOP6mo ago
this?
leowest
leowest6mo ago
yes
Core Dream
Core DreamOP6mo ago
ok, sorry
leowest
leowest6mo ago
and each view/vm would be 1 separate datatemplate of the views u want to be able to be swapped in there u can also connect the selection there to what was selected in a listbox if u want to make the menu react when u select something
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
im close
leowest
leowest6mo ago
No description
leowest
leowest6mo ago
well your settingsview is not called childview
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
yep fixed
leowest
leowest6mo ago
did u do the binding on the vm for Child
Core Dream
Core DreamOP6mo ago
i dont even have nothing to bind yet, settingsview is emoty
leowest
leowest6mo ago
u do
leowest
leowest6mo ago
No description
leowest
leowest6mo ago
u need to setup that if u want it to actually display
Core Dream
Core DreamOP6mo ago
so do i make dummy data pretty much in the vm?
leowest
leowest6mo ago
mmm no
Core Dream
Core DreamOP6mo ago
using CommunityToolkit.Mvvm.ComponentModel; namespace TitanSTUDIO.ViewModels; public partial class SettingsViewModel : ObservableObject { [ObservableProperty] private string _dummy = "Dummy Data"; } oh 😐
leowest
leowest6mo ago
👆 child sample there
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
ok so what is this BaseViewModel i dont even see code for it im getting too tired and making dumbass mistakes tbh
leowest
leowest6mo ago
ah its just the ObservableObject but instead I made a class that inherits from it as I tend to have things like Name and other properties that are common between
Core Dream
Core DreamOP6mo ago
ok changed it back to ObservableObject
leowest
leowest6mo ago
public partial class BaseViewModel : ObservableObject
{
[ObservableProperty]
private string _name;
}
public partial class BaseViewModel : ObservableObject
{
[ObservableProperty]
private string _name;
}
Core Dream
Core DreamOP6mo ago
what is this going to be used for?
leowest
leowest6mo ago
public partial class MainViewModel : BaseViewModel
{
public MainViewModel()
{
Name = "MainView";
}
}
public partial class MainViewModel : BaseViewModel
{
public MainViewModel()
{
Name = "MainView";
}
}
just an example its also a common class to be used if u need to have a List of VM or something
Core Dream
Core DreamOP6mo ago
but this is getting too confusing now
leowest
leowest6mo ago
or an object that can be different vms
Core Dream
Core DreamOP6mo ago
so many addons :/
leowest
leowest6mo ago
imagine this how would u be able to have SettingsViewModel, HomeViewModel, LoginViewModel inside Child is they have nothing in common
Core Dream
Core DreamOP6mo ago
ok
leowest
leowest6mo ago
BaseViewModel acts as the interpreter for that
Core Dream
Core DreamOP6mo ago
thats true
Core Dream
Core DreamOP6mo ago
so its like the glue between them all
leowest
leowest6mo ago
I will be back in 30 minutes or so food
Core Dream
Core DreamOP6mo ago
i need to sleep tbh, its past my bed time XD its why im messing up i cant focus
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
but there, a basemodel
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
and some error i guess but no problem, have a good one 🙂
leowest
leowest6mo ago
now u need SettingsViewModel to inherit from it every vm would inherit from BaseViewModel
Core Dream
Core DreamOP6mo ago
that does make more sense
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
😛
leowest
leowest6mo ago
it should build/run now
Core Dream
Core DreamOP6mo ago
now it can access both baseviewmodel and the data in there ok sec
leowest
leowest6mo ago
I dont see the last error
Core Dream
Core DreamOP6mo ago
No description
Core Dream
Core DreamOP6mo ago
getting somewhere what time is it by you?
leowest
leowest6mo ago
u have not made the Child property to set it to the settings so its just inheriting CHild from the MainViewModel and printing it 11
Core Dream
Core DreamOP6mo ago
yeah ill fix it in a bit its 10pm for me sadly 11am?
leowest
leowest6mo ago
pm
Core Dream
Core DreamOP6mo ago
ah you on tomorrow night at all? i cant function when my head is burnt out tbh never code when tired 🙂
leowest
leowest6mo ago
¯\_(ツ)_/¯ im usually around
Core Dream
Core DreamOP6mo ago
<ContentControl Grid.Row="0" Content="{Binding }"> <ContentControl.Resources> <DataTemplate DataType="{x:Type vm:SettingsViewModel}"> <local:SettingsView /> </DataTemplate> </ContentControl.Resources> </ContentControl> i just need to figurew out what to bind to ok np i will def get back to this tomorrow, it's useless to do this if i can't focus done that so many times too, doesn't help XD I appreciate your help though @leowest , learning alot tbh.
Want results from more Discord servers?
Add your server