AfterLife
AfterLife
CC#
Created by AfterLife on 11/3/2022 in #help
WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
Never the less thank you for your help! Have a wonderfull day/evening 🙂
35 replies
CC#
Created by AfterLife on 11/3/2022 in #help
WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
Yes indeed, the issue with that is that i don't know the restrictions of this project yet. This is going to be a Project for my Collage Module "Graphical User Interfaces" and the Proffesor hasn't disclosed rules about using Frameworks yet. https://github.com/xAfterLife/505-GUI
35 replies
CC#
Created by AfterLife on 11/3/2022 in #help
WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
It doesn't anymore.. That was my mistake.. Because I've had a flaw in thoughts in how to design my MainViewModel. Now that i correctly use MVVM everything works out fine!
35 replies
CC#
Created by AfterLife on 11/3/2022 in #help
WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
Beforehand the StartView wrote to a different instance of MainViewModel instead of StartViewModel
35 replies
CC#
Created by AfterLife on 11/3/2022 in #help
WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
And the StartViewModel Executes a Command like that
public static ICommand DummyViewModelCommand => new RelayCommand(_ => ChangeViewModel.ChangeView(ChangeViewModel.ViewType.Dummy));
public static ICommand DummyViewModelCommand => new RelayCommand(_ => ChangeViewModel.ChangeView(ChangeViewModel.ViewType.Dummy));
35 replies
CC#
Created by AfterLife on 11/3/2022 in #help
WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
The MainViewModel Subscribes like that
public MainViewModel()
{
ChangeViewModel.ViewChanged += (_, view) =>
{
CurrentView = view;
};
}
public MainViewModel()
{
ChangeViewModel.ViewChanged += (_, view) =>
{
CurrentView = view;
};
}
35 replies
CC#
Created by AfterLife on 11/3/2022 in #help
WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
I've ended up creating a "middle-man" class like that
using System;
using System.Collections.Generic;
using _505_GUI_Battleships.MVVM.ViewModel;

namespace _505_GUI_Battleships.Core;

public sealed class ChangeViewModel
{
public static event EventHandler<object>? ViewChanged;

public enum ViewType
{
Dummy = 0,
Start = 1
}

private static readonly Dictionary<ViewType, Type> Views = new()
{
{ ViewType.Dummy, typeof(DummyViewModel) },
{ ViewType.Start, typeof(StartViewModel) }
};

public static void ChangeView(ViewType viewType)
{
var view = Activator.CreateInstance(Views[viewType]);
if ( view == null ) return;
OnViewChanged(view);
}

private static void OnViewChanged(object e)
{
ViewChanged?.Invoke(null, e);
}
}
using System;
using System.Collections.Generic;
using _505_GUI_Battleships.MVVM.ViewModel;

namespace _505_GUI_Battleships.Core;

public sealed class ChangeViewModel
{
public static event EventHandler<object>? ViewChanged;

public enum ViewType
{
Dummy = 0,
Start = 1
}

private static readonly Dictionary<ViewType, Type> Views = new()
{
{ ViewType.Dummy, typeof(DummyViewModel) },
{ ViewType.Start, typeof(StartViewModel) }
};

public static void ChangeView(ViewType viewType)
{
var view = Activator.CreateInstance(Views[viewType]);
if ( view == null ) return;
OnViewChanged(view);
}

private static void OnViewChanged(object e)
{
ViewChanged?.Invoke(null, e);
}
}
35 replies
CC#
Created by AfterLife on 11/3/2022 in #help
WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
Sorry for the late reply, The issue was in my StartView that has a DataContext set to MainViewModel..
35 replies
CC#
Created by AfterLife on 11/3/2022 in #help
WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
Issue resolved.. I was just dumb trying to update a MainViewModel instance with another one..
35 replies
CC#
Created by AfterLife on 11/3/2022 in #help
WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
35 replies
CC#
Created by AfterLife on 11/3/2022 in #help
WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
Alternatively tried using the PresentationTraceSources.TraceLevel=High - Still the same no single Log message
<ContentControl Grid.Row="1" Content="{Binding CurrentView, PresentationTraceSources.TraceLevel=High }" />
<ContentControl Grid.Row="1" Content="{Binding CurrentView, PresentationTraceSources.TraceLevel=High }" />
35 replies
CC#
Created by AfterLife on 11/3/2022 in #help
WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
35 replies
CC#
Created by AfterLife on 11/3/2022 in #help
WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
Thank you, I'll take a look later today
35 replies
CC#
Created by AfterLife on 11/3/2022 in #help
WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
I thought i know but the issues i have gets me questioning myself right now. I'm confused about the Fact that the ContentControl loads the StartVm Viewmodel as the View I've assigned in the App.xaml correctly. Same happens when i use DummyVm as the initial Value in MainViewModel's Constructor. Only after changing the Value the issue persists, which is why i can't wrap my head around it. I can verify that the Value of the Binded Property Changed to the Correct Value; and the PropertyChanged event is being fired with "something" attached to it. If the initial Load works with the viewmodels as the values for "CurrentView"; shouldn't the same work via an Command updating the Binding?
35 replies
CC#
Created by AfterLife on 11/3/2022 in #help
WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
Almost forgot, ViewModels and Views are linked via App.xaml DataTemplates
<Application x:Class="_505_GUI_Battleships.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:_505_GUI_Battleships.MVVM.View"
xmlns:viewModel="clr-namespace:_505_GUI_Battleships.MVVM.ViewModel"
xmlns:mainView="clr-namespace:_505_GUI_Battleships"
StartupUri="MainWindow.xaml">

<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style/MenuButtonStyle.xaml" />
<ResourceDictionary Source="Style/TaskButtonStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
<DataTemplate DataType="{x:Type viewModel:MainViewModel}">
<mainView:MainWindow />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModel:StartViewModel}">
<view:StartView />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModel:DummyViewModel}">
<view:DummyView />
</DataTemplate>
</ResourceDictionary>
</Application.Resources>
</Application>
<Application x:Class="_505_GUI_Battleships.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:_505_GUI_Battleships.MVVM.View"
xmlns:viewModel="clr-namespace:_505_GUI_Battleships.MVVM.ViewModel"
xmlns:mainView="clr-namespace:_505_GUI_Battleships"
StartupUri="MainWindow.xaml">

<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style/MenuButtonStyle.xaml" />
<ResourceDictionary Source="Style/TaskButtonStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
<DataTemplate DataType="{x:Type viewModel:MainViewModel}">
<mainView:MainWindow />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModel:StartViewModel}">
<view:StartView />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModel:DummyViewModel}">
<view:DummyView />
</DataTemplate>
</ResourceDictionary>
</Application.Resources>
</Application>
I can only see the Value change in the Property
35 replies
CC#
Created by AfterLife on 11/3/2022 in #help
WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
By setting the
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
in code instead of xaml the EventHandler isn't null anymore. But the Content is still not updated even thou the Value of the Binded Property changed
35 replies
CC#
Created by AfterLife on 11/3/2022 in #help
WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
RelayCommand Class
public class RelayCommand : ICommand
{
private readonly Action<object?> _action;

public RelayCommand(Action<object?> action)
{
_action = action;
}

public void Execute(object? parameter)
{
_action(parameter);
}

public bool CanExecute(object? parameter)
{
return true;
}

public event EventHandler? CanExecuteChanged
{
add { }
remove { }
}
}
public class RelayCommand : ICommand
{
private readonly Action<object?> _action;

public RelayCommand(Action<object?> action)
{
_action = action;
}

public void Execute(object? parameter)
{
_action(parameter);
}

public bool CanExecute(object? parameter)
{
return true;
}

public event EventHandler? CanExecuteChanged
{
add { }
remove { }
}
}
ObservableObject Class
public class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;

protected void Update<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value))
return;
field = value;
OnPropertyChanged(propertyName);
}

private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;

protected void Update<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value))
return;
field = value;
OnPropertyChanged(propertyName);
}

private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
35 replies