I work to an application using MVVM pattern. In view i have a text box and a canvas. In canvas I will draw some lines, as many as the text box indicates. The text of text box is bind to a int property in view model. Also the text box has a command that updates the property in view model. I know how to rise property changed event on view model.
My question is: Is there a way to call the drawing method from view when property changed event is raised?
How about putting the canvas/drawing code in a separate user control with the number of lines as a dependency property bound to the ViewModel. You can then handle the dependency property change event inside the user control and do the drawing.
Related
I have an ItemsControl binding to a collection in the ViewModel. As a result of user input, a new item gets added to the collection and this gets displayed on the View.
The item is also a View-ViewModel pair, the View contains a TextBox that I would like to receive focus immediately after being added to the collection.
How do I set the focus to a TextBox without referencing the View from within the ViewModel? Are attached properties the way to go here?
well you can do this by creating a behaviour..
have a look here to get an idea of behaviours controlling focus
In a WPF/MVVM application I have a custom control on a particular view. This control extends WPF DataGrid and contains User names and ids.
On the same view I have some textboxes whose Text properties are bound to all different properties of User object exposed by the viewmodel and UpdateSourceTrigger for the Text properties are set to LostFocus.
Data gets updated as they should be whenever I leave a textbox (since the textbox looses focus). But problem is, this doesn't occur when I select any item in the custom control leaving any textbox, textbox data doesn't update. Can anyone explain what's happening?
Perhaps your custom control has its own focus scope defined, thus allowing logical focus to be in both the text box and your custom control? Try checking in snoop.
I can do element-to-element binding in WPF: For example, I've got a window that has a slider control and a textbox, and the textbox dynamically displays the Value property of the slider as the user moves the slider.
But how do i do this across separate windows (in the same project, same namespace)?
The reason is that my main application window containing the textbox has a menu option that will open an 'options' window containing the slider control.
You should use a (global) ViewModel, containing the data you need to share, and bind to the property from that ViewModel.
This way the changes in either of windows are reflected in the bound data object, and back.
You dont. Point. Databinding has to go to an element accessible in the same control.
What you can do is have the options menu bind go an object that it has in it's own code (property) that gets populated to the same object the othe rwindow uses as data source.
I am switching my UserControls via DataTemplate. When I leave the UserControl/DataTemplate View I need to ask wether the user wants to save or not because else all data will be lost like graphical location points x,y of a user drag/dropped Rectangle on a canvas.
How can I hook into the datatemplate change and bring up my own save dialog?
So you're binding a ContentControl's Content property to a property in your viewmodel?
The best place to implement this workflow would be where you normally set this viewmodel's property. Doing this in the viewmodel, rather than trying to hook into events in the View layer means you have more control and testability.
I have a custom text box control which raises a routed event when its TEXT property changes. This text property is data bound to a property on our view-model object.
When I place this control on a TabControl page or Expander control, it appears as if data binding only occurs when the control becomes visible for the first time, therefore I never receive any of the routed events until I swap to the tab the control is on or expand the expander.
Is there any way I can force data binding to occur before the control is shown?
Sounds like you relying on the data binding to genreate the routed event is the wrong approach. Instead you need to have your Model or ViewModel generate an event when the text is modified and then you watch this event from an appropriate place in your View.
Not very likely. WPF is a fairly efficient framework and won't do any work that it doesn't absolutely have to. This includes scenarios like data binding. Why bother exercising a collection for a control that might not ever be shown?