WPF DataContext … looking for the simplest syntax?

Up vote 2 down vote favorite 2 share g+ share fb share tw.

In the following XAML UserControl I am binding a few items to properties in the UserControl's linked class. In the linked class' contstructor I'm applying the DataContext of the items to this, as you can see below. Using System; using System.Collections.

Generic; using System. Linq; using System. Text; using System.

Windows; using System.Windows. Controls; using System.Windows. Data; using System.Windows.

Documents; using System.Windows. Input; using System.Windows. Media; using System.Windows.Media.

Imaging; using System.Windows. Navigation; using System.Windows. Shapes; namespace Kiosk { /// /// Interaction logic for EventSelectButton.

Xaml /// public partial class EventSelectButton : UserControl { public String ValueContainer; private String _EventImage; public String EventImage { get { return _EventImage; } set { _EventImage = value; } } private String _Label; public String Label { get { return _Label; } set { _Label = value; } } private String _SelectText; public String SelectText { get { return _SelectText; } set { _SelectText = value; } } public EventSelectButton() { InitializeComponent(); LabelTextBlock. DataContext = this; SelectTextBlock. DataContext = this; EventImageComponent.

DataContext = this; } } } Edit Although this works as intended, I'm interested to know if there is a simpler way of doing this. (edit, lessons learned. ) This won't actually work beyond the initialisation, the public properties will be set, however because the class doesn't use DependentProperties or alternatively, implement INotifyPropertyChanged, binding will not work as expected.

(end edit) For example, Can I set the DataContext of these items in the XAML to this (as the EventSelectButton instance), and if so, how? Alternatively, is it possible to inherit the DataContext from the UserControl parent, thus making the Binding Paths simpler. The only alternatives I've found so far are more verbose, e.g. Using the RelativeSource binding method to locate the EventSelectButton Ancestor.

So please, let me know any ways I can improve this binding expression, and any comments on best practices for binding within a UserComponent are much appreciated. Wpf xaml binding wpf-usercontrols link|improve this question edited Sep 9 '10 at 12:07 asked Sep 9 '10 at 3:19Slomojo6,53111227 100% accept rate.

One way is to do the following: Name your UserControl in your XAML. Bind the DataContext of the root element (i.e. Grid) to the UserControl.

Like this: . . .

Now, you'll ask, why not just set the DataContext of the UserControl itself? Well, this just ensures that setting the DataContext of an instance of the UserControl will still work without affecting the bindings in the UserControl's visual tree. So something like the one below will still work fine.

EDIT To make the solution complete requires the properties in the UserControl to be changed to use DependencyProperty objects instead. Below are the updates to the codes: XAML: Code-Behind: using System; using System.Collections. Generic; using System.

Linq; using System. Text; using System. Windows; using System.Windows.

Controls; using System.Windows. Data; using System.Windows. Documents; using System.Windows.

Input; using System.Windows. Media; using System.Windows.Media. Imaging; using System.Windows.

Navigation; using System.Windows. Shapes; namespace Kiosk { public partial class EventSelectButton : UserControl { public static readonly DependencyProperty EventImageProperty = DependencyProperty. Register( "EventImage", typeof(string), typeof(EventSelectButton)); public String EventImage { get { return (string)GetValue(EventImageProperty); } set { SetValue(EventImageProperty, value); } } public static readonly DependencyProperty SelectTextProperty = DependencyProperty.

Register( "SelectText", typeof(string), typeof(EventSelectButton)); public String SelectText { get { return (string)GetValue(SelectTextProperty); } set { SetValue(SelectTextProperty, value); } } public static readonly DependencyProperty LabelProperty = DependencyProperty. Register( "Label", typeof(string), typeof(EventSelectButton)); public String Label { get { return (string)GetValue(LabelProperty); } set { SetValue(LabelProperty, value); } } public EventSelectButton() { InitializeComponent(); } } }.

That's interesting, I tried doing ElementName binding expressions before I posted the question, and they didn't work. E.g. That's bound to this will not get updated.

Using dependency properties fixes this. An alternative, as I've suggested above, is to implement INotifyPropertyChanged. – karmicpuppet Sep 9 '10 at 5:32.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions