ListPicker SelectionChanged Property - c#

I am attempting to choose a specific item upon the SelectionChanged event of the ListPicker for WP8.I have two list pickers, I want to implement them such that when I select a particular item it changes the items on the other ListPicker. Any ideas how I could achieve this thanks in advance?

If you want to stay within the mvvm model, it might not be a bad idea to implement a attached property to invoke a command when the selection changes have a look at this post which shows an implementation:
ListBox SelectionChanged not getting called

Related

LongListSelector does not refresh using ObservableCollection

I have a simple LongListSelector without grouping option, lists some names. When a name changes in the source, the LongListSelector should have to update the list, but it shouldn't. Searching over the network I found that I have to use an ObservableCollection as data structure, beacause it has NotifyPropertyChanged event. Using an ObservableCollection instead of a List, nothing has changed: the LongListSelector does not update the items when I modify some name in the ObservableCollection.
The code is the same of this: http://code.msdn.microsoft.com/wpapps/LongListSelector-Demo-45364cc9
What should I modify to obtain an auto-update LongListSelector? I have to set NotifyPropertyChanged event or not? If yes, how?
The ObservableCollection auto updates when you change the collection itself and not a value inside one of its items.
It fires CollectionChanged when you add, remove etc... an item.
You should look at this especially simon's answer, so you can build a reusable object.

wpf: how to edit selected item with possible cancellation?

I basically want to create something like this:
So, a user can add/remove items from the list and edit them in the red panel below the list. When the item is selected, the changes can be made in the panel.
Then, the changes can be either saved or canceled with one of the buttons below. User cannot select another item in the list without explicitly cancelling the changes.
How to do this? I only see the option of making the red panel a separate control and changing its DataContext manually on ListView.SelectedItem changes. The red panel's DataContext is a special wrapper on a ListView's item which has 'save' and 'cancel' options. ListView is set to IsEnabled = False so that its SelectedItem doesn't change when editing is in progress.
How would you do this?
The question is quite close from, for example, this one :
How do I stop binding properties from updating?
Anyway the WPF Object that will handle this is the BindingGroup :
http://msdn.microsoft.com/en-us/library/system.windows.data.bindinggroup.aspx
the most easy apporach is to use a dialog for edit a selecteditem. thats what i do in my project. i use this dialogservice and handle the result.
if you want to handle all in one view you could set a property SelectionEnabled=false when the SelecteItem is set. and then SelectionEnabled=true when the save or cancel command is invoked.
the datacontext for your edit panel is simply your SelectedItem.
You can try to use bindings with UpdateSourceTrigger=Explicit. The blog post Edit With Explicit UpdateSourceTrigger will give you more information about how this can be implemented.

MVVM SelectedItem.Property OnChanged

I'm new with MVVM and I'm stuck...
I have a ListBox in the MainWindow. The ListBox contains Items of type WhatEverViewModel which are displayed by DataTemplates. The user can interact with these items and the WhatEverViewModel has several DependencyProperties which may change during interaction.
The question I have is:
How can I elegantly react (in the MainWindowViewModel) to changes of DependencyProperties of the CURRENTLY SELECTED WhatEverViewModel. I personally would implement some events in the WhatEverViewModel and when the SelectedItem of the ListBox change, I would attach to the events of the currently selected WhatEverViewModel. But I think in MVVM there might be a more elegant way to solve this...
Thank you.
Make CurrentWhatEver a property of your MainWindowViewModel and bind the Listbox.SelectedItem property on it. This way, MainWindowViewModel knows when the selected WhatEver changes and can register/unregister to events it's interested in.
communication between viewmodel can be done in several ways.
Messenger/Mediator like the one form MVVM Light
Eventstuff like the one from PRISM
or simply use harcoupling and subscribe to the events from the WhatEverViewModel in your mainviewmodel.
btw why in hell to you use DependencyProperties in your Viewmodels? simple Properties with INotifyPropertyChanged are the way to go.
one more thing. why you want to react to changes in the SelectedViewmodel(or better what you want to achieve, with the selected viewmodel.)? if you just want to display some information in your view, simply bind the SelectedViewmodel to it. you should specify your question in that way.
EDIT
The WhatEverViewModel has a list which is also bound to a listbox (in
datatemplate) and depending on what I select in the WhatEverViewModel
I want to display some kind of "Configurator" in the MainViewModel. –
JensPfister1 1 hour ago
why not simply bind the SelectedWhatEverViewmodel.SelectedListEntryProperty to your configurator view? can you post some code?
You should implement the INotifyPropertyChanged interface on each of your ViewModels. Then when one of your properties changes call the PropertyChanged event, your views will get notifications that the property has changed (as long as your Binding is correct). If a property is a list or collection make sure that the list is based off an INotifyCollectionChanged.
Add a property for the Selected WhatEverViewModel to your MainWindowViewModel, bind that in your ListBox. Then in your MainWindowViewModel you can hook on to the property changes of your Selected WhatEverViewModel.
For more guidance read:
WPF MVVM INotifyPropertyChanged Implementation - Model or ViewModel
http://blogs.msdn.com/b/soultech/archive/2011/06/07/mvvm-tips-when-implementing-inotifypropertychanged.aspx

WPF SelectionChanged event for all items in window?

I have a Window with many items in it, is there a Window or Grid Event to tell if the user has changed any textbox, combobox, radiobutton, checkbutton etc? I don't want to go through each item and add SelectionChanged event as this is just to tell if anything has changed since the data was last saved.
I think you should implement INotifyPropertyChanged interface which notify you once any property changed.
Here is an example that describes "Bind Better With INotifyProperty". This example is for a Windows App, but hope it would give you an idea.
Doing your job in this way is much elegant than adding events for each controls.

Detecting when a tab item was added to WPF Tab control

I am trying to synchronize the selected tab item of a WPF tab control with the last item that was added.
Since there is no such property as e.g. IsSynchedWithLastAddedItem, I am trying to detect when an item was added in order to point the SelectedItem at the last added one.
I cannot find the event that gets raised - either on the tab control or its Items, when a TabItem was added.
I am sure something like it must exist though, so I hope someone can help me out.
var view=CollectionViewSource.GetDefaultView(m_tabControl.ItemsSource);
view.CollectionChanged+=(o,e)=>{/*Here your code*/};
If you work directly with the Items-collection, the same technique will work also. Get the default CollectionViewSource for this collection.
var view=CollectionViewSource.GetDefaultView(m_tabControl.Items);
view.CollectionChanged+=(o,e)=>{/*Here your code*/};
As Timores wrote, for the m_tabControl.Items-property, you can attach a handler directly. The same is also true for most ItemsSource-references, but there you have to check yourself for the INotifyCollectionChanged-interface.
I have not tested it. Make a comment if it does not work.
The Items collection is of type ItemCollection, which derives from CollectionView which implements INotifyCollectionChanged. So you could listen to CollectionChanged and get to know when an item is added.
Don't know how to do that in XAML, though.

Categories

Resources