WPF TabControl add extra tabs to a bound control - wpf

I have a Tab Control with a ItemsSource Binding.
...
I want to add a predefined tab to the front called All that has an aggregate of all the other tabs, and I would also like to add a button at the end called Add so I can add a new tab.
Is there an easy way of doing this ?
Thanks,
Raul

The easiest way is to go with MVVM (the example in the url acutally contains TabControl bound to a ViewModel). Your ViewModel that you bind your TabPages against could expose an observablecollection of items where the first item is always a ViewModel instance that holds you aggregate data. All follwing items are the ViewModel instances for the rest of the tabpages. Your ViewModel would also expose a ICommand AddTabPage wich adds a new item to the obeservablecollection. The TabPage will pick up this change automatically. You'd have a button whose Command property is bound to this command.

Related

Binding to ViewModels that are displayed using a ContentPresenter and DataTemplates?

I am currently using an ItemsControl at the top of my application that contains a few buttons, bound to a list of ViewModelBases. These VMs are the pages of my app. When clicked, they set the current page to the appropriate page. A content presenter shows the current view model, and a DataTemplate converts it into the correct view.
How can I use Data Binding in these views? Since the view is being created by the ViewModel, doesn't that mean I can't create a new instance of that view model in the view? How can it find the correct instance to bind to? Or, is it automatically bound?
WPF ItemsControls automatically set the DataContext of each Item they render on screen to their corrsponding Item in the underlying collection. This means that, if you have an IEnumerable set as the ItemsSource of an ItemsControl (or any other ItemsControl-derived class, such as ListBox), it will render every item on screen (using any available DataTemplates) and automatically assign the DataContext property of the resulting visual elements to each of the ViewModelBase Items.

List<> Binding and button click using mvvm light

I am trying to use MVVM light to achieve something like this. I have the following scenario:
In my Model--I have set the properties like ActivityName,Image and there is a class constructor whose is accepting 2 parameters like name and image.
Im my DataAccess--I have set the database connection and implement the required method who will fetch the data from DB and I am storing in the List and returning the list to ViewModel.
In my ViewModel--I have created the list property who will return the list by calling the GetActivities() method that I have defined in the DataAccess.
Now my problem is I am not getting how to bind it in the View so that by clicking on a button it will display the list of activities with image. By clicking on some button a new window should open with the desired result. How to bind the above list and implement the button functionality using MVVM light.
Kindly help?
Thanks
First of all, use an ObservableCollection instead of a List as it would notify the view when property or collection changes.
Then set the DataContext of your view to the viewmodel. If you use MVVMLight View Class, then the DataContext would be automatically set. You've to just give the ViewModel Name there.
Then set the ItemsSource of the DataGrid like this <dg:DataGrid ItemsSource="{Binding YourListInViewModel}"/>
For handling click event you can use the Event-To-Command behaviour and write your logics in the Button's corresponding Command handler.
Bind to DataContext of the control

How to set a value in a ViewModel from binding?

Kind of an odd question- if I'm thinking of this the wrong way please let me know. I am using an infragistics dock manager, which manages tabs as well. So I can create a TabGroupPane, and then add multiple ContentPanes, each of which has its own tab.
In each content pane, I set my viewmodel:
<ContentPane>
<viewmodels:MyViewModelForTab1 />
</ContentPane>
So here's the problem- while using a mediator pattern for communication, my viewmodels have no idea if they are on the visible tab or not, so they are always working even if hidden. The TabGroupPane does have a SelectedTab property, as well as each ContentPane having an IsActive property.
So the question is how do I set that information in my ViewModel? Making my VM a dependency object seems like a bad idea since I already implement INotifyPropertyChanged. Using a CLR prop in my VM also doesnt work, since you cannot bind to it.
How can I get my VM to know if it is the datacontext of an active tab?
Thanks!
I would put an IsSelected property on my ViewModel and bind it to the TabItem's IsSelected dependency property.
This should allow you to hook into when it is updated and perform whatever you need. You don't need a mediator pattern here since you are communicatining from the View to the ViewModel.
Make sure your ViewModel is bound to the DataContext property of the view (specifically, that the Tab's DataContext is the ViewModel). The way you have it now, your ViewModel is the content of the element, not bound to the DataContext, as it should be:
<Tab.Resources>
<viewmodels:MyViewModelForTab1 x:Key="Tab1ViewModel" />
</Tab.Resources>
<ContentPane DataContext="{StaticResource Tab1ViewModel}" />
Or something like that...
I don't know the Infragistics model, so my apologies if this is inapposite, but here's how I implement this with regular items controls - tab control, list box, whatever.
Create a container view model class that includes an observable collection of items and exposes a SelectedItem property. Make the container class the data context of the items control. Bind the items control's SelectedItem property to the container class's.
Hook the item objects up to the PropertyChanged event of the container. So now when the selected item in the UI changes, the container view model notifies all of the items that SelectedItem has changed. Each item object's event handler can determine for itself whether or not it's now the selected item.
The item objects thus don't know any implementation details of the UI - you can unit test your classes outside of a UI and the logic will still work correctly.

Sharing state between ViewModels

I have two ViewModels that present the same Model to different Views. One presents the model as an item in a ListBox, the other presents it as a tab in a TabControl. The TabControl is to display tabs for the items that are selected in the ListBox, so that the tabs come and go as the selection changes.
I can easily synchronise the two controls by adding an IsSelected property to the Model and binding the ViewModels to it (a bit like this), but this will clutter the Model with presentation details that don't really belong there.
It seems like I need something between the Model and ViewModels to hold this extra state. Are there any patterns or examples of a good way to do this?
Use a ViewModel.
You've got a View that contains the two controls. Have a view model that will contain a list of ViewModels for the ListBox control to bind to. Also within this view model bind the listbox selection to a second list of viewmodels that the TabControl then also binds to.
That way your listbox drives what the tab control shows without this information entering the model which should stay oblivious to the existence of the view.
TabControl is ItemsControl, so you shouldn't be shy to bind its ItemsSource to ListBox.SelectedITems.
Obviously ViewModel for List should have a property that would produce ViewModel for Tabs:
public TabViewModel ItemTabModel { get { ... } }
And because TabControl is a bit funny, you'd need to add ItemContainerStyle to populate Content for TabControlItem, because the normal ItemTemplate for TableControl only affects headers for tabs.

autoupdate a list in wpf

I want to display a bunch of Objects i have created in a ListBox. My objects implement the INotifyPropertyChanged Interface.
I tried to use an ObservableCollection, which i have bound to a listbox Control (listbox1.DataContext = MyCollection)
But this does not exactly what i want to do, because the Listbox is not refreshed when one of the properties of one of my objects in MyCollection changes.
I have found this blogposting: http://sweux.com/blogs/psampaio/index.php/2009/04/13/creating-a-custom-observable-collection-in-wpf
is this realy the easyiest/only way to keep track of several objects?
I'm not sure, but have you tried using a datatemplate for your listbox items? like, a textbox that explicitly sets it's text to the appropriate binding.

Resources