Binding a viewModel within a frame

Eduardo Gomez 3,416 Reputation points
2023-02-08T01:12:36.4333333+00:00

I have a MainWindow, that has several pages, and a frame to navigate

All my pages have their ViewModel.

This is the code to navigate

        private void PasgeSelectionItem(ModernWpf.Controls.Frame frame) {
            frame?.Navigate(SelectedItem?.NavLink);

            if (SelectedItem!.Name.Equals("CntactUS")) {
                SendEmail();
            }
            if (SelectedItem.Name.Equals("acc")) {
                if (!string.IsNullOrEmpty(UserId)) {
                    try {
                        ProfilePage profile = new ProfilePage() {

                            DataContext = new ProfilePageViewModel(UserId)
                        };
                        frame?.Navigate(SelectedItem.NavLink);
                    } catch (Exception ex) {
                        Debug.WriteLine(ex.Message);
                    }

                }

The problem is that even though I set up my view Model for the profile page, and passed the UserID when it navigates, my bindings don't work

<Grid
        Margin="20"
        Background="DarkBlue">
        <Grid.RowDefinitions>
            <RowDefinition Height="100" />
            <RowDefinition Height="20" />
            <RowDefinition Height="60" />
            <RowDefinition Height="60" />
            <RowDefinition Height="60" />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Ellipse
            Width="100"
            Height="100"
            HorizontalAlignment="Center"
            VerticalAlignment="Top">
            <Ellipse.Fill>
                <ImageBrush ImageSource="/Images/Placeholder.png" />
            </Ellipse.Fill>
        </Ellipse>

        <Separator Grid.Row="1" />

        <TextBox
            Grid.Row="2"
            Margin="20,0,20,0"
            VerticalAlignment="Center"
            ui:ControlHelper.CornerRadius="10"
            ui:ControlHelper.PlaceholderText="{x:Static res:Lang.Username}"
            Foreground="Red"
            Text="{Binding UserData[0].Object.Username}" />
        <TextBox
            Grid.Row="3"
            Margin="20,0,20,0"
            VerticalAlignment="Center"
            ui:ControlHelper.CornerRadius="10"
            ui:ControlHelper.PlaceholderText="{x:Static res:Lang.FirstName}"
            Foreground="Red"
            IsReadOnly="{Binding IsReadOnly}"
            Text="{Binding UserData[0].Object.FirstName}" />
        <TextBox
            Grid.Row="4"
            Margin="20,0,20,0"
            VerticalAlignment="Center"
            ui:ControlHelper.CornerRadius="10"
            ui:ControlHelper.PlaceholderText="{x:Static res:Lang.LastName}"
            Foreground="Red"
            IsReadOnly="{Binding IsReadOnly}"
            Text="{Binding UserData[0].Object.LastName}" />


        <Button
            Grid.Row="5"
            Margin="20,0,20,0"
            HorizontalAlignment="Right"
            VerticalAlignment="Center"
            ui:ControlHelper.CornerRadius="10"
            Content="{Binding BtonContent}" />

I also tried to send y userID to the profile page and then to the vm, but I get Null exemption when navigating

Code: https://github.com/eduardoagr/TranscribeMe

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2023-02-08T07:47:25.5666667+00:00

    Hi,
    Navigate method create new instance of page. Please, try this code in MainWindowViewModel:

    		private void PasgeSelectionItem(ModernWpf.Controls.Frame frame)
    		{
    			frame?.Navigate(SelectedItem?.NavLink);
    
    			if (SelectedItem!.Name.Equals("CntactUS"))
    			{
    				SendEmail();
    			}
    			if (SelectedItem.Name.Equals("acc"))
    			{
    				if (!string.IsNullOrEmpty(UserId))
    				{
    					try
    					{
    						if (frame != null)
    						{
    							frame.LoadCompleted += (s, e) => ((ProfilePage)e.Content).DataContext = e.ExtraData;
    							frame.Navigate(SelectedItem.NavLink, new ProfilePageViewModel(UserId));
    						}
    					}
    					catch (Exception ex)
    					{
    						Debug.WriteLine(ex.Message);
    					}
    
    				}
    			}
    		}
    
    

0 additional answers

Sort by: Most helpful