Conditional binding using mvvm light on wp7 - c#

Hi, Please consider the custom wp7 message box above. I am looking to see what is the cleanest way to bind a views textbox to a property on the view model only after a button is clicked. The only way I can work out is to have two properties and use a command on the button to assign the value of the first prop to the main prop. The main prop should only receive the value from the textbox only if the user clicks the tick button and not if they cancel (by pressing the back button).
I am using MVVM Light.

What about catching the click event and sending the changed text from View to ViewModel via Messenger. On ViewModel the property would be only a getter.

I think that the way you do it is ok.
Have a temporary binded property and main property synced with the temporary one only when messagebox is accepted.

Related

Text box for searching MVVM

I'm learning WPF C # with a simple application that I'm trying to do. I'm wondering how do I create a text box to search. For example: The user enters any name and as he enters the application shows the similar names below the gift box of the text box.
Researched on some sites, but found people writing much code in the Code-Behind and this solution does not interest me.
Since you are interested in MVVM, this is my idea:
bind a string in to the view model to the Text property of the TextBox
implement INotifyPropertyChanged in the view model
implement view model's PropertyChanged event in the model (presenter) and trigger some action on every change (meaning on every change of the contents of the TextBox)
Make sure that the binding on the TextBox is set to Mode=TwoWay (although I belive that is already by default)

How to programmatically click a button in WinRT?

How is this possible for WinRT?
I have read the answer for WPF How to programmatically click a button in WPF?, but does not really help me.
Anyone solved such an issue?
First, I'd like to say if you want the button to "appear to have been pressed (in terms of animation/highlight effects) this won't help you but otherwise it should.
My Advice to you would be to follow the Model-View-ViewModel (MVVM) design pattern when designing your application if you haven't already. That way instead of calling the "button" click you can simply execute the method in your viewmodel that would normally be bound to that click.
Example:
You create a model class representing data in your database.
You create a view (page/window) with buttons and other UI elements on it.
You create a ViewModel class that has a series of public methods and collections.
Now in the XAML for the View, you bind the ViewModel as your DataContext and bind the public properties of the ViewModel to your collections (ItemSource for a ListBox being bound to an ObservableCollection is on example). You can create public methods that are "commands" and bind them your buttons so that when the button click event is fired, the command in the view model is executed. Now for all your unit tests and for any other reason you might want to programmatically "click" the button, you can simply call the associated methods in the ViewModel and never worry about what the actual View is doing.

WPF RaisePropertyChanged event on lost focus

I have a C# WPF MVVM application that works fine.
The only problem is when I modify a textbox and click on the menu. If I do that without clicking on another control, the view->viewmodel event is never fired because the textbox hasn't lost focus. Correct me if I am wrong, but I think the RaisePropertyChanged is only fired on LostFocus (or OnBlur, or any similar event).
So, clicking on the menu save button right after editing the textbox causes the viewmodel to save the data using old values.
So, resuming:
This sequence works fine:
Edit the text box
Click on another control
RaisePropertyChanged is fired, the viewmodel is updated
Click on save button on the menu
Data Saved with correct values
This sequence gives me an error:
Edit the text box
Click on save button on the menu
Data Saved with correct values
How to solve this?
This is a common gotcha with TextBoxes in both WPF and WinForms. You can get around this by instructing the binding system to update the VM with every change to the TextBox instead of when it loses focus. To do this, set the UpdateSourceTrigger of the binding to PropertyChanged. This will write back to the VM any time the TextBox raises the PropertyChanged event for its Text property.
<TextBox Text="{Binding MyText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
For the TextBox.Text dependency property, its default UpdateSourceTrigger is LostFocus (ie, your view model property gets updated when the control loses focus). To make the property update immediately whenever text is entered, set UpdateSourceTrigger=PropertyChanged. (See the link above for more info -- it actually covers your example specifically.)

WPF C# Change button content from child window

I'm developing a touchscreen app. I have a form with buttons with the content "new player" and then a keyboard with a textbox is shown for the user to enter his name. Now I need that when I close (or while inputting the name) the button from the parent window change the content to the name that the user typed. But I cant make a binding to the parent.. how can I do it??
Are you implementing the Model-View-ViewModel pattern in your WPF application, or using databinding at all?
You don't have to use MVVM, but you should really be using databinding in some fashion.
With databinding, you'd bind the button content to a string property of some object (a view-model in MVVM). That object needs to implement the INotifyPropertyChanged interface. You can bind the textbox on the child window to the same property of the same object.
With that in place, the button will be updated automatically when the user types in a new player name.
If you need more details, please comment and I'll be glad to elaborate.
You can try something like:
var button = this.Parent.Controls.OfType<Button>().Select(b => b.Name == "NameOfControl").First();
button.Content = name;
Edit: And Jay's idea is the right way at going at this, i was just offering an alternative way of accessing the controls from their parent using LINQ.

How to raise property changed events of two user controls in MVVM

in my view, i have two chart controls of type MyChart:
MyChart1
MyChart2
The chart user control has a button called Refresh. Clicking on the button refreshes their item source and they display new data.
In the ViewModel of the view, I have two properties of type MyChart, one for each MyChart.
When I click on the Refresh button, how do I raise RaisePropertyChanged event of the view model of the view?
This is not a correct implementation of MVVM, as you have application logic coded into the View layer.
The standard approach would be to have a Command property on your ViewModel, then bind Button.Command to the ViewModel.Command. This will allow you to handle the refreshing in the ViewModel and give you a place to write any additional code you need to write.
To answer your question, if you are using MVVM, the properties displayed in the View actually exist in the ViewModel, so you should be able to handle PropertyChanged easily enough in the ViewModel using this.PropertyChanged += new PropertyChangedEventHandler(ViewModel_PropertyChanged);

Categories