Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a label in my mainwindow that I am trying to update from a user control viewmodel. But no matter what I have tried it does not update.

These are the fields in the mainwindow.
<Grid Height="30" Width="190" HorizontalAlignment="Center" Margin="900, 145, 20, 0" VerticalAlignment="Top">
        <StackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Right" Height="25" Margin="0, 0, 0, 0" Width="190">
        <Label x:Name="lblFederal" HorizontalAlignment="Right" Content="{Binding Amts.FederalTotal}" Width="90" Height="25" FontSize="10pt" />
        <Label x:Name="lblState" HorizontalAlignment="Right" Content="{Binding Amts.StateTotal}" Width="90" Height="25" Margin="15, 0, 0, 0" FontSize="10pt" />
    </StackPanel>
</Grid>


What I have tried:

Here is my code from my user control viewmodel.

   MainWindow _mainWindow;
   MainWindowVM _mainWindowVM;

_mainWindow.lblFederal.Content = "20";
_mainWindowVM.Amts.FederalTotal = "20";
_mainWindowVM.updateAmounts(20, 20);
Posted

1 solution

You are using Data binding in you Xaml. Your properties on your ViewModel should using INotifyPropertyChanged events. You cna read more here: Data binding overview - WPF .NET | Microsoft Learn[^]

Here are a couple of tutorial links for you:
* WPF - Data Binding - Tutorials Point[^]
* Introduction to WPF data binding - The complete WPF tutorial[^]
 
Share this answer
 
v2
Comments
Maciej Los 27-Nov-23 11:01am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900