Silverlight databinding a textBlock in another user control - silverlight

I have a small usercontrol that basically increments or decrements a value by one. The user control has two buttons(one to add and the other to subtract) and a textBlock that is used to display the value.
I am going to have multiple instance of this usercontrol in another usercontrol so I can manipulate values of a dataclass that has an INotifyPropertyChanged interface. My question is how can I databind the textBlock of the value changing usercontrol to the usercontrol I instansiated it in?

First, I want to state that Silverlight 2 does not support element to element binding. That feature is added in Silverlight 3 (out in Beta now). Having said that, I don't think you want to bind controls together anyway. It sounds like you're trying to build a NumericUpDown control and you probably have some class in code behind that's actually doing the incrementing and decrementing.
If that's the case, you can simply subscribe to the click handlers and call a method on your model like Increment or Decrement. Your model can expose a property for the current value and that property is what is bound to your text box.
Now if you're actually trying to build a NumericUpDown control, you might want to check out the Silverlight Toolkit. The toolkit already includes this control and it also supports data binding.
Check out the NumericUpDown Control here and download the toolkit here.
Finally, binding from a child control to a parent control really isn't any different. The parent UserControl has a DataContext and all child controls inherit that. Each individual child control can also have its DataContext set. Binding expressions are always relative to the DataContext and the DataContext can be set in code. In your case, probably to a model of some sort.
I hope that helps.

Related

WPF Control Grouping

I have a group of controls that look like this:
<Link to Image>
that i reuse a number of times. It's really simple a listview, 3 buttons and some layout panels.
I want to turn this into a reusable component but the columns in the listview can change and the sources they are bound to will change.
How do i go about this? i've seen many comparisons between ContentTemplates and UserControls etc but they never seem to be functional (eg Add will raise an event which i'll have to handle to add something to the listview, remove will raise an event where i'll likely ask if they are sure first).
I've accomplished the events with my own UserControl, but can't pass a list of GridViewColumns to the control. It also means i have to expose SelectedItem etc manually from the UserControl. Subclassing Listview seems promising for setup and access but doesn't conceptually seem right to have other controls in the listview area.
What is the right way?
I would definately recommend a UserControl. You should:
Add the controls you require to your user control
Add the Dependency Properties you require to your user control which allow you to configure it, e.g. SelectedItem
Wire up these dependency properties to the various controls within your user control. An easy way to do this is to set the DataContext of your user controls visual tree to the user control itself, e.g. if you have a Grid as the root for your user controls, set its DataContext = this in code. You can then use TwoWay bindings to connect up the various control properties to the user control properties.

Forcing a repaint of a WPF control

I currently try to create classes for a paint-like WPF application. I have to base classes LineMovement (line from StartPoint to EndPoint) and PathMovement (line going through all points specified in a property Points of type PointCollection). These classes inherit from Control and get their looks through a ControlTemplate.
The ControlTemplate also adds an Adorner to the AdornerLayer of the Movement objects containing a little visual marker for every moveable point of the specific line. These markers support dragging with a mouse.
The problem I have is that somehow my Movement classes don't repaint when their points are moved. I debugged my code with Mole and found out that the Polyline used to visualize the line gets the changed point values (visible in its Points property) but it just doesn't repaint.
How can I force a repaint of a WPF control?
TemplateBinding doesn't support two-way data binding (i.e. updating the Points collection with the new values of the Polyline). It's meant only for one-way data binding for use in control templates. See Bea Stollnitz's blog entry: http://bea.stollnitz.com/blog/?p=38
Turns out that TemplateBinding is pure evil.
When I bind the Points of the Polyline by {TemplateBinding Points} it doesn't update itself, whereas when I bind it with {Binding RelativeSource={RelativeSource TemplatedParent}} it works perfectly.
Note to myself: Never use this goddamn TemplateBinding again.
You need to make your Movement objects' DPs have the AffectsArrange metadata property (http://msdn.microsoft.com/en-us/library/system.windows.frameworkpropertymetadataoptions.aspx) - that way when the property changes, WPF knows it should redraw

Hook in datatemplate view switch/change

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.

Different data contexts in a SL3 form

I wrote a custom Silverlight 3 control that uses a class as its data context (MVVM pattern). I want to place this control on another control (form) through XAML. The child control exposes a Dependency Property that when set through XAML, will make it show detailed info.
So an example is that the child control shows order details data, and I want to place it on a form that show user orders. When you select an order, the selected item value on the parent control (orders list), is data bound to the child control, to show details.
The problem is that the child control’s dependency property's OnChanged handler never gets called. If I do not set a data context on the child (so it uses the parent's data context) all works fine, but when I set a different data context, it breaks down.
Ideally, your ViewModel would be for the outer UserControl and a property on the ViewModel will be the DataContext of the Inner/Child userControl
Its true that when the Parent control's DataContext is set, it is propogated down to all its child controls. But the child control has an option of overriding this behavior by setting its own DataContext (which you seem to be doing in your example). Hence by the rule of preferences, the child control's DataContext is given more preference and thus it overrides the parent's one. Also since the child's DataContext never changes after it's initially set, the DP never gets invoked.
So I thought about this some more, and I understand what is happening, but I think its very confusing, and is not done right. If I am doing data binding on a control in the main page, it should use the context of that page to do the binding. And binding I do inside the control should use the control's context.
The way it works now uses the control's context no matter where I put the binding expression (unless I am doing E2E binding, then its using the main page's context). That is silly to me. But at least I understand it now.
I solved the problem using Element to Element binding, and got it to work. I hope the SL team would change this behavior.

How can I get WPF binding to occur on controls which have not yet been shown?

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?

Resources