locked
Selected item Display On Another page

    Question

  • public class MyModel : ViewModel.ViewModelBase
        {
            private string name;
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                    RaisePropertyChanged("Name");
                }
            }
            private int age;
            public int Age
            {
                get
                {
                    return age;
                }
                set
                {
                    age = value;
                    RaisePropertyChanged("Name");
                }
            }
            private string lastName;
            public string LastName
            {
                get
                {
                    return lastName;
                }
                set
                {
                    lastName = value;
                    RaisePropertyChanged("LastName");
                }
            }
        }

     class MyViewModel : ViewModelBase
        {
            private ObservableCollection<MyModel> mymodelList;
            public ObservableCollection<MyModel> MymodelList
            {
                get
                {
                    return mymodelList;
                }
                set
                {
                    mymodelList = value;
                    RaisePropertyChanged("MymodelList");
                }
            }
    
    
            private MyModel addtoMyModel ;
            public MyModel AddtoMyModel
            {
                get
                {
                    return addtoMyModel;
                }
                set
                {
                    addtoMyModel = value;
                    RaisePropertyChanged("AddtoMyModel");
                }
            }
            private MyModel selectedModel;
            public MyModel SelectedModel
            {
                get
                {
                    return selectedModel;
                }
                set
                {
                    selectedModel = value;
                    RaisePropertyChanged("SelectedModel");
                }
            }
    
            
            #region Adding Data
            private void AddMyModel()
            {
                MymodelList.Add(new MyModel
                    {
                        Age = AddtoMyModel.Age,
                        Name = AddtoMyModel.Name,
                        LastName = AddtoMyModel.LastName,
                    });
            }
            public RelayCommand AddMyModelCommand { get; set; }
            #endregion
    
            #region Delete Selected Item
            private void Delete()
            {
                if (SelectedModel != null)
                {
                    MymodelList.Remove(SelectedModel);
                }
            }
            public RelayCommand DeleteCommand { get; set; }
            #endregion
    
            #region Delete All Data
            private void DeleteAll()
            {
                MymodelList.Clear();
            }
            public RelayCommand ClearAllCommand { get; set; }
            #endregion
    
            #region Navigate To Edit Page
            private  void Edit()
            {
                if(SelectedModel != null)
                {
                    var rootFrame = Window.Current.Content as Frame;
                    rootFrame.Navigate(typeof(EditPagePage1)); 
                    
                }
            }
            public RelayCommand EditCommand { get; set; }
            #endregion
    
            #region Selected Item Edit And Update
            private void Update()
            {
                if (SelectedModel != null)
                {
                    selectedModel.Age = addtoMyModel.Age;
                    selectedModel.Name = addtoMyModel.Name;
                    selectedModel.LastName = addtoMyModel.LastName;
                }
    
                var rootFrame = Window.Current.Content as Frame;
                rootFrame.Navigate(typeof(MainPage)); 
                
            }
            public RelayCommand UpdateCommand { get; set; }
            #endregion
            
            #region Constructs
            public MyViewModel()
            {
                AddtoMyModel = new MyModel();
                SelectedModel = new MyModel();
                MymodelList = new ObservableCollection<MyModel>();
                AddMyModelCommand = new RelayCommand(AddMyModel);
                ClearAllCommand = new RelayCommand(DeleteAll);
                DeleteCommand = new RelayCommand(Delete);
                EditCommand = new RelayCommand(Edit);
                UpdateCommand = new RelayCommand(Update);
            }
            #endregion

    <Page
        x:Class="MVVMTEST.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:MVVMTEST"
        xmlns:viewmodels="using:MVVMTEST.ViewModel"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Page.DataContext>
            <viewmodels:MyViewModel>
    
            </viewmodels:MyViewModel>
        </Page.DataContext>
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <ListView HorizontalAlignment="Left" Height="428" Margin="28,34,0,0" VerticalAlignment="Top" Width="532"
                      ItemsSource="{Binding MymodelList}" SelectedItem="{Binding SelectedModel, Mode=TwoWay}" SelectionMode="Single" >
                      <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Width="400" Background="Chocolate">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Name}" FontSize="30" />
                            <TextBlock Text="," FontSize="30" />
                            <TextBlock Text="{Binding LastName}" FontSize="30" />
                        </StackPanel>
                        <TextBlock Text="{Binding Age}" FontSize="30" />
                    </StackPanel>
                </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <Button Content="Add"  VerticalAlignment="Top"
                    Command="{Binding AddMyModelCommand}" Margin="804,386,0,0" Width="175"/>
            
            <TextBlock Text="Name" FontSize="30" Margin="807,39,394,666" Height="63"/>
            <TextBox DataContext="{Binding  AddtoMyModel}" Text="{Binding Name, Mode=TwoWay}" FontSize="30" Margin="1038,36,30,666"/>
            <TextBlock Text="Last name" FontSize="30" Margin="807,157,357,548"/>
            <TextBox DataContext="{Binding AddtoMyModel}" Text="{Binding LastName, Mode=TwoWay}" FontSize="30" Margin="1038,145,30,561"/>
            <TextBlock Text="Age" FontSize="30" Margin="807,255,362,441"/>
            <TextBox DataContext="{Binding AddtoMyModel}" Text="{Binding Age, Mode=TwoWay}" FontSize="30" Margin="1032,255,30,444"/>
            <Button Content="Clear All" Command="{Binding ClearAllCommand}"
                    HorizontalAlignment="Left" Margin="1029,386,0,0" 
                    VerticalAlignment="Top"/>
            <Button Content="Delete" Command="{Binding DeleteCommand}" 
                    HorizontalAlignment="Left" Margin="1170,386,0,0" VerticalAlignment="Top"/>
            <Button Content="Edit" Command="{Binding EditCommand, Source={Binding SelectedModel}}" 
                    HorizontalAlignment="Left" Margin="804,473,0,0" VerticalAlignment="Top"/>
        </Grid>
    </Page>
    

    <Page
        x:Class="MVVMTEST.View.EditPagePage1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:MVVMTEST.View"
        xmlns:viewmodels="using:MVVMTEST.ViewModel"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Page.DataContext>
            <viewmodels:MyViewModel>
    
            </viewmodels:MyViewModel>
        </Page.DataContext>
        
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"  >
            
            <TextBlock Text="Name" FontSize="30" Margin="807,39,394,666" Height="63"/>
            <TextBox DataContext="{Binding AddtoMyModel}"   Text="{Binding SelectedModel.Name, Mode=TwoWay}"  FontSize="30" Margin="1038,36,30,666"/>
            <TextBlock Text="Last name" FontSize="30" Margin="807,157,357,548"/>
            <TextBox DataContext="{Binding AddtoMyModel}" Text="{Binding SelectedModel.LastName, Mode=TwoWay}" FontSize="30" Margin="1038,145,30,561"/>
            <TextBlock Text="Age" FontSize="30" Margin="807,255,362,441"/>
            <TextBox DataContext="{Binding AddtoMyModel}"  Text="{Binding SelectedModel.Age, Mode=TwoWay}" FontSize="30" Margin="1032,255,30,444" AcceptsReturn="True"/>
    
            <Button Content="Update" Command="{Binding UpdateCommand}" 
                    HorizontalAlignment="Left" Margin="804,473,0,0" VerticalAlignment="Top" Width="171"/>
            
        </Grid>
    </Page>
    

    I want Selected Item Show on Edit Page Textbox and Update Button click update and shown main page listView
    Wednesday, June 4, 2014 6:51 AM

Answers

  • You actually are facing issues with data binding. You can get a working solution here. Below listed are the few changes that i did:

    1. Accessed the ViewModel as a Singleton instance. 
    private static MyViewModel _myViewModel;
    public static MyViewModel GetInstance()
            {
                return _myViewModel ?? (_myViewModel = new MyViewModel());
            }

    2. In both views, assigned DataContext explicitly to the singleton instance:

    public MainPage()
            {
                this.InitializeComponent();
                this.DataContext = MyViewModel.GetInstance();
            }
    
    public EditPagePage1()
            {
                this.InitializeComponent();
                this.DataContext = MyViewModel.GetInstance();
            }
    An alternate approach to this step could be using an IOC container. You can find more details here.

    3. Fixed some data binding issues in EditPagePage1.xaml

    4. Commented out few unwanted operations in MyViewModel.

    private void Update()
            {
                //if (SelectedModel != null)
                //{
                //    selectedModel.Age = addtoMyModel.Age;
                //    selectedModel.Name = addtoMyModel.Name;
                //    selectedModel.LastName = addtoMyModel.LastName;
                //}
    
                var rootFrame = Window.Current.Content as Frame;
                rootFrame.Navigate(typeof(MainPage)); 
                
            }



    - Ram
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, June 5, 2014 9:05 AM

All replies

  • Please just provide the project on OneDrive and put the link here.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, June 4, 2014 3:08 PM
    Moderator
  • Thursday, June 5, 2014 2:22 AM
  • You actually are facing issues with data binding. You can get a working solution here. Below listed are the few changes that i did:

    1. Accessed the ViewModel as a Singleton instance. 
    private static MyViewModel _myViewModel;
    public static MyViewModel GetInstance()
            {
                return _myViewModel ?? (_myViewModel = new MyViewModel());
            }

    2. In both views, assigned DataContext explicitly to the singleton instance:

    public MainPage()
            {
                this.InitializeComponent();
                this.DataContext = MyViewModel.GetInstance();
            }
    
    public EditPagePage1()
            {
                this.InitializeComponent();
                this.DataContext = MyViewModel.GetInstance();
            }
    An alternate approach to this step could be using an IOC container. You can find more details here.

    3. Fixed some data binding issues in EditPagePage1.xaml

    4. Commented out few unwanted operations in MyViewModel.

    private void Update()
            {
                //if (SelectedModel != null)
                //{
                //    selectedModel.Age = addtoMyModel.Age;
                //    selectedModel.Name = addtoMyModel.Name;
                //    selectedModel.LastName = addtoMyModel.LastName;
                //}
    
                var rootFrame = Window.Current.Content as Frame;
                rootFrame.Navigate(typeof(MainPage)); 
                
            }



    - Ram
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, June 5, 2014 9:05 AM
    • THANK You Very Much ....................................................
    • THANK You Very Much ....................................................THANK You Very Much ....................................................
    • THANK You Very Much ....................................................THANK You Very Much ....................................................THANK You Very Much ....................................................THANK You Very Much ....................................................THANK You Very Much ....................................................
    Thursday, June 5, 2014 9:21 AM
  • Hi Please tell me how this code working , i am check that working but how is it . it just Navigating  another page code only .

    what is this code also

    public static MyViewModel GetInstance()
            {
                return _myViewModel ?? (_myViewModel = new MyViewModel());
            }

    Thursday, June 5, 2014 10:49 AM
  • Hi Please tell me how this code working , i am check that working but how is it . it just Navigating  another page code only .

    what is this code also

    public static MyViewModel GetInstance()
            {
                return _myViewModel ?? (_myViewModel = new MyViewModel());
            }

    I'm just using that ViewModel as a Singleton class. You can find more details about singleton classes here.



    - Ram
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, June 5, 2014 12:02 PM