• (2 Pages)
  • +
  • 1
  • 2

[WPF] Exploring MVVM (Model View ViewModel) II

#16 tcba  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 31-January 17

Posted 01 February 2017 - 03:11 AM

View Postandrewsw, on 01 February 2017 - 01:10 AM, said:

The functionality that is needed is already present within the application. Double-clicking won't achieve anything yet.


Thanks again - yes, I have just discovered too. Double click doesn't action. Can you please give me some pointers? I'm thinking there needs to be an action code in mainwindow that sends a command to go to the other view with the selectedItem ID? in MainWindow code behind there is (but this would only get me the table view..
		private void btnAllItems_Click(object sender, RoutedEventArgs e)
		{
			if (_listT == null)
			{
				_listT = new VehicleTableVM(_list);
			}
			this.DataContext = _listT;
		}



ToDoTableVM in represents ToDoListVM into a tale row, but I do not see any action to allow a click to that will take that selectedItem and traverse to the other data template of ToDoListVM:

Here si the tableVM code: I can see that it is getting the existing lists and filling the table, but that's it

		private VehicleListVM _vehicleList;

		public VehicleTableVM(VehicleListVM ExistingList)
		{
			VehicleList = ExistingList;
		}
		public VehicleListVM VehicleList
		{
			get
			{
				return _vehicleList;
			}
			set
			{
				if (value != null)
					_vehicleList = value;
			}
		}
	}



And in Mainwindow.xaml there's no 'view item' button or such on the table data template. The only place I see that has some reference to location of a collection item is in ToDoListVM but that is only to paginate the CollectionViewSource.

        <DataTemplate DataType="{x:Type viewmodels:ToDoTableVM}">
            <ListView x:Name="TheList" ItemsSource="{Binding Path=ToDoList}" 
                      IsSynchronizedWithCurrentItem="True">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Title" Width="200" 
                                        DisplayMemberBinding="{Binding Path=Title}" />
                        <GridViewColumn Header="Start Date" Width="120" 
                                        DisplayMemberBinding="{Binding Path=StartDate}" />
                        <GridViewColumn Header="Due Date" Width="120" 
                                        DisplayMemberBinding="{Binding Path=DueDate}" />
                        <GridViewColumn Header="Completed Date" Width="120" 
                                        DisplayMemberBinding="{Binding Path=CompletedDate}" />
                    </GridView>
                </ListView.View>
            </ListView>
        </DataTemplate>




Thanks
Was This Post Helpful? 0
  • +
  • -

#17 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6340
  • View blog
  • Posts: 25,569
  • Joined: 12-December 12

Posted 01 February 2017 - 03:35 AM

Look at the answer here at stackoverflow. This adds a ListView.ItemContainerStyle property that will respond to clicking an item in the table.

This property needs to be added to the DataTemplate for ToDoTableVM which is in Mainwindow.xaml, here <DataTemplate DataType="{x:Type viewmodels:ToDoTableVM}">.

The code that responds to the click is added in Mainwindow.xaml.cs. What this code needs to do is to check that an item is selected (this is the code shown at stackoverflow) and then change the DataContent of the main window: this.DataContext = _list;. This is the code that is already attached to the ToDoItem button that I mentioned earlier.

What this code does is to change the DataContext for the Window to _list, which is the instance of ToDoListVM.

Changing the context triggers the alternative DataTemplate for ToDoListVM (<DataTemplate DataType="{x:Type viewmodels:ToDoListVM}">), meaning that the table view is replaced with the individual view (DataTemplate) for a ToDo item.



Quote

I do not see any action to allow a click to that will take that selectedItem and traverse to the other data template of ToDoListVM

Once the click event is set up as I've described, simply changing the DataContext will cause one DataTemplate to be used in preference to another, causing the required change to the window.
Was This Post Helpful? 0
  • +
  • -

#18 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6340
  • View blog
  • Posts: 25,569
  • Joined: 12-December 12

Posted 01 February 2017 - 03:43 AM

Actually, a response to the SO answer was this, "Actually, you can set the handler directly on the ListView, there is no need for an EventSetter." which you may wish to investigate.

The second answer outlines how this could be achieved by setting up a Command infrastructure, rather than using code-behind, if you wanted to pursue this.
Was This Post Helpful? 0
  • +
  • -

#19 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6340
  • View blog
  • Posts: 25,569
  • Joined: 12-December 12

Posted 01 February 2017 - 04:01 AM

[I got this working as I have outlined, with just a little bit of copying and pasting.

It did require two clicks though, so I switched to previewing ButtonUp rather than ButtonDown. This does mean that clicking an empty area would take us to the single view for whichever item happens to be currently selected.. but at least it works.

Requiring a double-click would be better.]
Was This Post Helpful? 0
  • +
  • -

#20 tcba  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 31-January 17

Posted 03 February 2017 - 03:25 AM

View Postandrewsw, on 01 February 2017 - 04:01 AM, said:

[I got this working as I have outlined, with just a little bit of copying and pasting.


Yes, so did I! Thanks so very much mate. You're an awesome teacher. I cannot thank you enough.

I think my biggest hurdle has been that I don't have the vocabulary and to search for what I need to know, nor do I know enough (yet) to know if what I am looking for is the solution.

You're the best

Thanks again!
Was This Post Helpful? 1
  • +
  • -

#21 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6340
  • View blog
  • Posts: 25,569
  • Joined: 12-December 12

Posted 03 February 2017 - 04:12 AM

Thank you.
Aw shucks!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2