Dave
Dave

Reputation: 15016

Databinding to non-UI elements

I'm very new to databinding, and so far have only had luck databinding to element properties in the GUI, or by binding to an ObservableCollection to display lists of data. Now I want to do something different.

I have a method that takes an index and returns a List of doubles, i.e.

List<double> GetValues( int index);

I have a GUI where the user selects the index from a combobox, and then several textboxes need to have their text updated with the values in the List. I actually have a thread running that caches all of this data, because I have UI elements in different places that consume and display the same information. So I figured, why not use databinding? The problem is, I have yet to find a good example online that explains how to take the index from the combobox, call the GetValues method, and then bind the resulting information to all of the textboxes -- all from XAML.

The closest article I've found is http://msdn.microsoft.com/en-us/magazine/cc163299.aspx. Most of the articles I've read talk about using the Source attribute, but then say, "well, the easiest way is to just use StaticResource, so we'll show you that method".

How can this be accomplished? Would it be advisable to just go back to the easy way of doing this entirely from code-behind?

Upvotes: 0

Views: 913

Answers (1)

Will Eddins
Will Eddins

Reputation: 13907

The problem you're having is that you're trying to bind to the results from a function, and you may very well complicate things by trying to implement data-binding on something so simple from the code-behind. I'd recommend doing this from the code-behind.

That said, for the simplest and most useful approach, you need to have actual properties in your class to bind to, which you need to update when the index changes. Depending on how you pass the data around, this could reduce the code-behind, or just create more.

Here's an example of what you could end up with:

// Assume 1 of your textboxes displays a weight. Here's the property declaration:
// Disclaimer: Not compiled or tested at all.
public static readonly DependencyProperty WeightProperty = DependencyProperty.Register(
  "Weight", typeof(double), typeof(MyClass), new PropertyMetadata(0.0));

public double Weight
{
  get { return (double)this.GetValue(WeightProperty); }
  set { this.SetValue(WeightProperty); }
}

// Here's an example of setting the property:
private void ComboBoxSelectedIndexChanged(object sender, RoutedEventArgs e)
{
  List<double> values = myObject.GetValues(comboBox.SelectedIndex);
  this.Weight = values[0];
}

// And in your XAML, assuming you've given your Window the name myWindow:
<TextBlock Text="{Binding ElementName=myWindow, Path=Weight}"/>

This can be useful if you plan on updating the Weight property in multiple places and want the TextBlock to always show the correct value.

On the other hand, if your properties will only update in the SelectedIndexChanged function and you don't need the values outside of that function, you may as well have just set the value yourself and reduce the unnecessary overhead:

private void ComboBoxSelectedIndexChanged(object sender, RoutedEventArgs e)
{
  List<double> values = myObject.GetValues(comboBox.SelectedIndex);
  txtWeight.Text = values[0].ToString();
}

Upvotes: 2

Related Questions