Hi, i'm having a rough fight with WPF and its Binding system. I just wanna bind a property of an instance called HPP, this property is pressure and this object exist in MainViewModel, so in MainWindow.xaml a have this:
In SetComponent:
In MainWindow.xaml.cs:
public partial class MainWindow : Window, INotifyPropertyChanged
{
private readonly Variables GV = Variables.GetInstancia();
private MainViewModel _mainViewModel;
public MainViewModel MainViewModel
{
get { return _mainViewModel; }
set
{
_mainViewModel = value;
OnPropertyChanged();
}
}
public MainWindow()
{
InitializeComponent();
GV.MainViewModel.StartAssign(GV);
MainViewModel = GV.MainViewModel;
GV.MainViewModel.MainWindow = this;
DataContext = MainViewModel;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
I tried with RelativeSource without success. If i create a variable in MainViewModel it works. Any help?
Mohamed Azarudeen ZPosted Jul 12, 2023, 3:08 PM
Hi Fenix
It seems like you are facing difficulties with the binding system in WPF when trying to bind a property of an instance called HPP in MainViewModel. Here are a few suggestions to help you troubleshoot the issue:
1. Verify DataContext: Double-check that the DataContext of your MainWindow.xaml is set correctly. In your case, it should be set to MainViewModel. You've already set DataContext = MainViewModel in the MainWindow constructor, so ensure that MainViewModel has a valid instance and the property HPPRoute exists and is properly initialized.
2. Check Property Change Notification: Ensure that the HPPRoute property in MainViewModel is implementing the INotifyPropertyChanged interface and raising the PropertyChanged event when its value changes. This is crucial for the binding system to detect and update the UI.
3. Verify Property Path: Ensure that the property path in your XAML binding expression is correct. In this case, double-check if HPPRoute.Pressure is the correct path to the Pressure property you want to bind to. If the property is nested within another object, verify the object hierarchy and property names.
4. Debugging: You can set breakpoints or add logging statements in the PropertyChanged event handler or the setter of the HPPRoute property to verify if the value is being updated correctly and if the PropertyChanged event is raised.
5. Verify SetComponent UserControl: Make sure that the SetComponent UserControl is correctly defined and that the VariableValue dependency property is properly implemented. Ensure that the UserControl's DataContext is set correctly to the MainViewModel.
By carefully reviewing these points, you should be able to identify the issue and resolve the problem with the binding system in your WPF application.
If you find this answer usefull kindly accept.