C
C#2w ago
Shock

WPF local: when using Dependency Injection

Hello, My WPF app is utilizing dependency injection following https://achraf-chennan.medium.com/net-core-dependency-injection-for-wpf-core-62dc282690f8 (which removes StartupUri from the App.xaml file). When I'm attemping to utilize data binding, but local:SessionConverter isn't being recognize and I'm not understanding how to work around it as I'm unable to set the DataContext in the main window due to the parameters.
public enum Data
{
Data1,
Data2,
Data3
}
public partial class MainWindow : Window
{
private readonly ICustomWrapper _customWrapper;
public MainWindow(ICustomWrapper customWrapper)
{
InitializeComponent();
}
private Data _data;
public Data data
{
get=> _data;
set
{
_data = value;
MessageBox.Show("The selected datum is: " + "value.ToString());
}

public class SessionConverter : IValueConverter
{
public object? Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value?.Equals(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value?.Equals(true) == true ? parameter : Binding.DoNothing;
}
}
public enum Data
{
Data1,
Data2,
Data3
}
public partial class MainWindow : Window
{
private readonly ICustomWrapper _customWrapper;
public MainWindow(ICustomWrapper customWrapper)
{
InitializeComponent();
}
private Data _data;
public Data data
{
get=> _data;
set
{
_data = value;
MessageBox.Show("The selected datum is: " + "value.ToString());
}

public class SessionConverter : IValueConverter
{
public object? Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value?.Equals(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value?.Equals(true) == true ? parameter : Binding.DoNothing;
}
}
The .xaml code looks like the following (button data removed)
<Window x:Class="DataTest.MainWindow"
xmlns:local="clr-namespace:DataTest"/>
<Window.Resources>
<local:SessionConverter x:Key="SessionConverter"/>
</Window.Resources>
<Grid>
<StackPanel>
<RadioButton>
</RadioButton>
</StackPanel>
</Grid>
<Window x:Class="DataTest.MainWindow"
xmlns:local="clr-namespace:DataTest"/>
<Window.Resources>
<local:SessionConverter x:Key="SessionConverter"/>
</Window.Resources>
<Grid>
<StackPanel>
<RadioButton>
</RadioButton>
</StackPanel>
</Grid>
Could anyone point me in the right direction here?
Medium
.NET Core Dependency Injection for WPF Core
Short story about Dependency Injection
9 Replies
Buddy
Buddy2w ago
Don't keep the data in the Window but prefer to use MVVM, especially if you're using design patterns already. You rarely ever need a direct reference to the window
Shock
ShockOP2w ago
I plan on having the user make a data selection on the MainWindow which allows me to later direct the program based on that input. I would need the direct reference in this situation no?
Buddy
Buddy2w ago
Have you used bindings before?
Shock
ShockOP2w ago
I have not which is probably why I should stop trying to run before crawl. I'm repurposing an old VB program and WPF looked like a great design upgrade as the prior file is just an exe that runs based on the current window. I'm attempting to make the user select their functionality instead of reading the active window In my earlier attempts before reseraching MVVM I was taking the result of the Radio Button selection and parsing the string in order to set a variable, but it appears Data Binding is the best practice for that
Buddy
Buddy2w ago
Oh. Yeah. The structure of WPF generally aims towards using MVVM. However you can use it like winforms, etc. But that'll make expanding the project a pain. Especially if it's a larger project
Shock
ShockOP2w ago
That's where I ended up as well. It's probably not "needed" but because of the UI customization that WPF allows I wanted to dive into that instead of Winforms - however it seems like winforms would have probably been the better choice here Although from what I'm seeing here - while it violates MVVM - I would be able to have my features work if I just didn't use bindings and violated that architecture Although I don't plan on having to greatly expand the project once it's complete
Buddy
Buddy2w ago
It's really good to aim for MVVM / XAML because modern frameworks do use XAML and it's fairly easy if you use MVVM to port it in the future since the data doesn't rely on the framework itself
Shock
ShockOP2w ago
I see. So de-coupling the data selection from MainWindow will ensure I can safely do that then?
Buddy
Buddy2w ago
It should, yes.
Want results from more Discord servers?
Add your server