WPF data Context Switching issue at runtime @HuiLiu-MSFT

saurabh alliumsepa 20 Reputation points
2023-11-04T07:16:32.4966667+00:00

Hi @Hui Liu-MSFT ,

I have a project in the drive:

https://drive.google.com/file/d/1R66IiO2-Duo5NWjbOGNBBoc-COvu9mAF/view?usp=drivesdk

Problem is I am unable to switch the DataContext (Tblist[0] and Txtblocklist[0]) at runtime on clicking of individual control elements .i.e. If TextBox is clicked, then properties linked to TextBox must be displayed in the property grid control of HandyControl at runtime. If TextBlock is clicked, then properties linked to TextBlock Must be displayed. You will understand when you open the project. Kindly help me and how to do that . Since the project is big. That's why shared the link ? Thank you.


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,710 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 47,341 Reputation points Microsoft Vendor
    2023-11-16T06:46:59.91+00:00

    Hi,@saurabh alliumsepa . To achieve navigation between two views, each bound to its own ViewModel, and ensure that data changes are retained when switching between views, you could use a Frame element in the main window and navigate between the views within that frame. Here's a simple example using the MVVM pattern:

     <Window.Resources>
         <DataTemplate DataType="{x:Type local:View1ViewModel}">
             <local:View1/>
         </DataTemplate>
         <DataTemplate DataType="{x:Type local:View2ViewModel}">
             <local:View2/>
         </DataTemplate>
     </Window.Resources>
     <Window.DataContext>
         <local:MainWindowViewModel />
     </Window.DataContext>
     <StackPanel>
         <StackPanel Orientation="Horizontal" Height="30" VerticalAlignment="Top">
             <Button Content="View 1" Command="{Binding GotoView1Command}" />
             <Button Content="View 2" Command="{Binding GotoView2Command}" />
         </StackPanel>
             <Frame Content="{Binding CurrentView}" />
         
       
     </StackPanel>
    

    View1:

    <UserControl.DataContext>
        <local:View1ViewModel/>
    </UserControl.DataContext>
    <StackPanel>
        <TextBlock Text="Number1" Margin="5" />
        <TextBox Text="{Binding Text1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Margin="5" />
        <TextBlock Text="Number2" Margin="5" />
        <TextBox Text="{Binding Text2,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Margin="5" />
        <TextBlock Text="Addition of Number1 and Number2" Margin="5" />
        <TextBox Text="{Binding Sum,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" Margin="5" />
        <Button Content="Increment Text1" Command="{Binding IncreaseMyText1Command}" Margin="5" />
        <Button Content="Increment Text2" Command="{Binding IncreaseMyText2Command}" Margin="5" />
        <TextBox Margin="5" />
       
    </StackPanel>
    
    
    

    View1.cs:

    
     public partial class View1: UserControl
     {
       public View1()
       {
         InitializeComponent();
       }
     }
     public class View1ViewModel : ViewModelBase
     {
       #region Text1
       private int MyText1 = 30;
    
       public int Text1
       {
         get
         {
           return MyText1;
         }
         set
         {
           MyText1 = value;
           OnPropertyChanged(nameof(Text1));
           OnPropertyChanged(nameof(Sum));
         }
       }
    
       #endregion
    
       #region Text2
       private int MyText2 = 40;
    
       public int Text2
       {
         get
         {
           return MyText2;
         }
         set
         {
           MyText2 = value;
           OnPropertyChanged(nameof(Text2));
           OnPropertyChanged(nameof(Sum));
         }
       }
    
       #endregion
    
       #region Sum
       //int _sum = 0;
       public int Sum
       {
         get
         {
           try
           {
             return Text1+ Text2;
           }
           catch (FormatException)
           {
             return 0;
           }
         }
    
       }
       #endregion
       private ICommand _increaseMyText1Command;
       private ICommand _increaseMyText2Command;
      
    
       public ICommand IncreaseMyText1Command
       {
         get
         {
           return _increaseMyText1Command ?? (_increaseMyText1Command = new RelayCommand(
              x =>
              {
                IncreaseMyText1();
              }));
         }
       }
       public ICommand IncreaseMyText2Command
       {
         get
         {
           return _increaseMyText2Command ?? (_increaseMyText2Command = new RelayCommand(
              x =>
              {
                IncreaseMyText2();
              }));
         }
       }
    
       private ICommand _navigateToView2Command;
    
    
    
       public View1ViewModel()
       {
        
       }
       private void IncreaseMyText1( )
       {
         Text1++;
       }
       private void IncreaseMyText2()
       {
         Text2++;
       }
     
     }
    
    

    View2:

    
    <Grid>
        <TextBox Name="tb" Text="hello view2" />
        <!--<Button  
                Content="Goto View 1"  Command="{Binding GotoView1Command}"            
                HorizontalAlignment="Center"     Margin="10,10,0,0"  
                VerticalAlignment="Center"  Width="75"></Button>-->
    </Grid>
    
    
    

    The restul:

    6


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful