Silverlight MVVM binding seems not to work - silverlight

Building my first SL MVVM application (Silverlight4 RC) and have some issues i don't understand.
Having a WPF background i don't know what is going on here:
ViewModel has several properties, in which one is called SelectedRecord. This is a get only property and is defined like this:
public Culture SelectedRecord {
get { return culturesView.View.CurrentItem as Culture; } }
As you can see it is gets the current value of a CollectionViewSource (called culturesView). So if i select a Culture, the SelectedRecord (gets a value directly from within the CollectionViewSource) as expected. (Actually there is a datagrid control bound to the CollectionViewSource, hence it is possible to change the selected item)
OK. Now to the View . There are several views which access this ViewModel and in particular there is one which shows the values of the aforementioned property SelectedRecord (let's call it the EditView). To show this EditView there is a button (which has its Command property bound to an ICommand in the ViewModel) which functions (the first time) as expected.
This means:
1st try : i select a record, switch to EditView, outcome: selected record values are shown (as expected!!).
2nd try: switch back to datagrid, select another record, switch to EditView, outcome: the values of the previous shown record are shown again!!! WHY??
First i thought that the SelectedRecord has not the correct value set, but i was mistaken: it HAS the correct value! So it should be shown!?
What am i missing? In WPF this would work!!
Thanks in advance

When CurrentItem value changes, your ViewModel that has SelectedRecord must call RaisePropertyChanged("SelectedRecord") so whatever View is bound to it is notified about the change.

Related

ZK framework: I have two Comboboxes; How to clear the 2nd Combobox on selecting a different item on the first Combobox?

I am new to zk framework and I tried everything but nothing seems to work; I used
#Command
public void clearSelection(#BindingParam("listModel")ListModelList model) {
if (model!=null) {
model.clearSelection();
}
}
and on the 2nd combobox I used:
onChange="#command('clearSelection', listModel=self.model)";
It is clearing the text of the second combobox but the list is not being populated on the second combobox; I want when the value of the first combobox changes then the text that is displayed on the second combobox to be removed
the above function clears the text but the data is not getting binded on the second combobox
i want:
combobox 1 -> (value)
combobox 2 -> (value)
combobox 1 -> (another value)
combobox 2 -> previously displayed data to get clear whilst the data
pertaining to (another value) to be displayed
I tried using #bind instead of #load but cannot comeup with a solution
Thankyou in advance
For those who new to ZK, I recommend using MVC pattern (using Composer) because it's easy to start.
when the value of the first combobox changes then the text that is displayed on the second combobox to be removed
onChange="#command('clearSelection', listModel=self.model)"
This command binding should fulfill this requirement.
but the data is not getting binded on the second combobox
I don't see you bind any model to the 2nd combobox. I suppose there should be 2nd model like:
<combox model="#init(vm.model2)"/>
Besides, you usually don't need to pass a ListModel into a command method like:
public void clearSelection(#BindingParam("listModel")ListModelList model)
Since the ViewModel should already have that ListModel object.

Silverlight RadComboBox keeping open after selection

Silverlight MVVM. I have a RadCombobox, and for each selection I'm adding a new row in another datagrid. I add necessaries codes in my ViewModel class and this part is working. What I’d like to perform is:
Keep the comboBox open while the control has the focus in order to allow the user repeating selection (I bind IsDropDownOpen to a method and setting in SelectedItem property to true but still it closes after selection )
Unselect the Item selected to allow duplication selection. I added the event SelectionChanged and add code in MainPage.xaml.cs but looking for a solution within my ViewModel.
Lets say,
IsDropDownOpen = {Binding IsDropDownFromViewModel}
Also, assuming that the getter of IsDropDownFromViewModel is encompassing all your conditions for the drop down to be open, and will always return the correct drop down state.
Now all that you will need to do is fire the PropertyChanged event for this property wherever/whenver you think the drop down should have been open, but is closed, or vice versa.
Unfortunately I didn't get your exact scenario, but lets assume this is the case (You should be use a similar approach to fix whatever problem you have).
Example Scenario:
The drop down closes when you select an item, it is intended to stay open
In the above case, one the user selects an item, the setter for the selectedItem's corresponding binding property should be invoked, so that is where we write the notification code
public SelectedItemType SelectedItemInViewModel {
get{
return _selectedItemVM;
},
set{
_selectedItemVM=value;
NotifyPropertyChanged("IsDropDownFromViewModel");
}
}
What this does is, it will tell the radComboBox's IsDropDownOpen property to reevaluate it's binding expression on the RHS and get its new value
Hope you get the gist of the approach, if not leave a comment.

INotifyPropertyChanged not working as expected

I have a little trouble with INotifyPropertyChanged. I implemented it lots of times, however this case is different. I would like to create the following application:
There are multiple tab containers, which are bound to different ObservableCollections of Entities. Now I would like to show the name of the currently (last) selected Entity. To achieve this, I've created created a DependencyProperty CurrentEntity in the Window class and assign via SelectionChanged on the TabControls the current Entity.
private void SelectionChanged(Object sender, EventArgs e)
{
CurrentEntity = e.NewItems[0] as Entity;
}
The TextBlock which, in the above picture, displays VideoA looks like this:
<TextBlock Text={Binding CurrentEntity.Name}" />
Now whenever I select a new Tab, the Text in the TextBlock changes accordingly. However when I change the the name of an Entity (inside a TextBox in a Tab, which is also bound to the Entity), the text in the TextBlock doesn't change. I have to select another Tab and then change back to the Tab where I changed the Name to see the newly entered name in the TextBlock. Basically, this is my problem, I would like to see the text changing without selecting another Tab first (and yes, I de-focused from the TextBox inside the Tab after entering the new Name).
Has anyone an idea where the problem is?
My "architecture" looks (more or less ;-)) like this:
ObservableCollection just means that the event to update UI is kicked off when the collection changes, you add, remove, re-order, etc.
Altering an the property of an item in an ObservableCollection, such as a string, doesn't not actually cause a UI event.
Does the object you have inside the ObservableCollection implement INotifyPropertyChanged?

WPF: MVVM - Disable command attached button on ivalueconverter convert exception

I have a simple scenario and a issue with it which I just cant seem to resolve past few days.
OK, first of all I use MVVM to bind my View on a ViewModel. I have in my view several text boxes which binds to several properties (most strings) in ViewModel (binds actually to an Custom Object (type Person, name SelectedPerson) with strings properties , object which is a property of the viewmodel). This object implements INotifyPropertyChanged and IDataErrorInfo. It has also an int property named Age. I also have in my view a button which is bound to a command in my viewmodel, a command which inside CanExecute test the SelectedPerson's properties and return true if all are correct.
Now my issue is: if I put in my Age text box from my View something not int, a red tectagle will appear (is normal, because there is an exception to the conversion), but in that specific moment, to the object behind (SelectedPerson, type Person) there isn't sent the newValue (the setter to that property Age, or the IDataErrorInfo Members don't intercept the value .... I guess it is normal because there isn't any "new" value, because I put an incorrect format in the text box in the first place).
So, maybe I repeat myself, the issue is: if the new Age (new incorrect Age) isn't set, then the Object behind still hold last value, which if it was correct then the command itself it's correct (the can execute will return true) and the button is enabled
As you can imagine I want the submit button (it's a button which saves current person details in data storage module) to be disabled when current properties don't pass through conversions methods.
PS: I used a IValueConverter class , and on that text box binded to Age, I made use of my StringToIntConverter class....but on Convert Method I don't know how to pass the SelectedPerson binded object (I just pass the text value, and return the int value)
I guess one way to do it could be by using MultiBinding scenario , but I'm not sure.
If I could pass the SelectedPerson inside Convert method from that converter I could invalidate that command from the converter itself.
Sorry for my English, I know it's far from perfect :) and thanks in advance for your time.
I think the cleanest solution would be to bind the textbox to a string property instead, and update your view model so that your IDataErrorInfo implementation for that property name attempts a string to int conversion and returns the result of that. Your CanExecute test would also then include this conversion as part of the validation test.

Error handling for AutoCompleteBox with Silverlight 3 + MVVM Light toolkit

I'm trying to create a UserControl that contains a AutoCompleteBox. I want to use the SelectedItem property of this AutoCompleteBox to populate other UserControls with information based on which item the User selected.
To prevent the SelectedItem to be fired every time a user "navigate" between items in the drop-down I've created a EventToCommand that executes on DropDownClosed event like this:
The command is of type:
public RelayCommand SelectedItemCommand { get; private set; }
This works fine except for when a user start typing something that has a match at the beginning, but if the user continue typing and there is no match anymore, then the DropDown is closed and no item is actually selected. This gives me an error that says:
Unable to cast object of type 'System.Windows.RoutedPropertyChangedEventArgs`1[System.Boolean]' to type 'MyProject.ViewModels.MyItem'
I tried to make a try-catch statement in the Command method for the command, but the exception seems to be fired even before I get into that method.
My question is:
How can I prevent the command from beeing fired if there is no-match (that is, no actual SelectedItem in the AutoCompleteBox)?
Are you using PassEventArgsToCommand? In that case, the RelayCommand must be RelayCommand<EventArgs> and the CommandParameter may not be used. It is a limitation of the ICommand interface, which can only have one CommandParameter. Annoying, I know, but usually I am able to use a different way to get what I want (for example by binding the SelectedItem to a property on my VM with a TwoWay binding).
Let me know,
Laurent

Resources