C#C
C#14mo 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;
    }
}


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>


Could anyone point me in the right direction here?
Medium
Short story about Dependency Injection
Was this page helpful?