Tim
Tim

Reputation: 1

Databinding with parameters

I'm currently trying to develop a small finance application. My application needs a quite complex GUI (basically a table with dynamic rows and columns as well as categories and subcategories); since I couldn't think of a standard xaml-way to achieve that, I'm writing a custom control that is supposed to display my data.

So long story short, I'm creating a lot of different WPF UI elements in C# code directly, not in XAML. Now I'll need a way to synchronize some of these component's values with dynamically changing data. An obvious example is that some of my text boxes (or labels or whatever) will need to display the price of a stock. It should thus be bound to another class which provides up to date prices.

My main problem is that I don't know in advance which prices will be needed and where they should be displayed. So what I'd need is basically a way for my textbox to contact my price-object and ask "hey, i'm going to need a price for stock US0123456789. Please tell me that price and keep updating my value whenever the price of that stock is updated."

All databinding examples I found were usually focused on binding to the variables within a specific object instance, but I can't simply bind to, say, PriceObject.Price because there can't be a simple "Price" variable. After all, there's going to be various different stocks managed by the price object and each text box only needs the price of one specific stock, so it'd need to bind to something like PriceObject.PriceForStock("US0123456789"). I couldn't find a way to bind to a method, though, and I don't know how how else it'd be possible.

The only way I could think of was to have a Dictionary in my GUI-class witch stock ISINs as its keys and a reference to the UI Element as value. Then my GUI could observe the Price Object and each time prices change, and event is triggered and the GUI class could update the UI Element manually, something like (pseudo code incoming):

private Dictionary<string, UIElement> displayed_stocks = new Dictionary<string, UIElement>();

private void create_gui()
{
   Textbox textbox = new Textbox();
   displayed_stocks.Add(isin, textbox);
}

private void receive_change_event_of_price_object(string affected_isin, double new_price)
{
  displayed_stocks[affected_isin].Text = new_price.ToString();
}

But I don't know, I feel like this is a sloppy solution. Especially because I won't only need dynamic prices but also other dynamic information like portfolio holdings, which will rely not only on ISIN but on ISIN and portfolio ID, so it wouldn't be that simple with a dictionary. Is there any better way to do it? I'd be glad to be pushed in the right direction there! Thanks

Upvotes: 0

Views: 942

Answers (2)

Vinit Sankhe
Vinit Sankhe

Reputation: 19885

Dynamic Bindings are achieved using...

  1. Indexer as Bas has already said above. Even ToString() can do the trick too.
  2. MultiBinding with IMultiValueConverter deciding which binding source(s) should take effect. MultiBinding has an advatnage that you can bind all possible things that can be displayed in your TextBox and send them to the converter (whether available or not) and then the converter will decide which one to mould and display.
  3. If the sequence of priority of source values is fixed then you can use PriorityBinding. No need for the converter discussed above.

Upvotes: 0

Bas
Bas

Reputation: 27085

If you create an indexer on the object you bind to you can bind with a parameter in a clean and easy manner, see this link for more information.

Upvotes: 1

Related Questions