0

I have got a class named StockItem.
The class contains some instance properties including Quantity.
The class contains some static properties including Cart and CartTotalPrice.
Cart type is ObservableCollection.
CartTotalPrice returns the cart total price.

I have created a UserControl named UCOrder allowing the user to add items into the cart.
I have created another UserControl allowing the user to change the quantity of a cart item.

I have declared a TextBlock in the markup of UCOrder.
I want to bind the TextBlock to the static property StockItem.CartTotalPrice so the TextBlock displays the current CartTotalPrice.
How can I do it ?

Any help will be greatly appreciated

3
  • 2
    I don't understand how CartTotalPrice and Cart could be static. If they are, it would mean all StockItem object are associated with the same unique Cart. Could you please post the code of those two classes ? Commented Jun 3, 2013 at 10:38
  • Create a non-static observable property which internally uses the static one and bind to that; that being said, as Marshall777 points out, statis properties is a bad idea in general. Commented Jun 3, 2013 at 10:39
  • @stijn How do you create an observable property ? Commented Jun 3, 2013 at 11:56

1 Answer 1

2
<TextBlock Text="{Binding Source={x:Static StockItem.CartTotalPrice}}"/> 

It might seem odd at first, but Source tells us which is the actual source of our binding, which is usually the DataContext, but we setit to use our static property. With path we would now supply a property on the source object, but because we already have in source what we want, we don't set path which is by default "." which means "Take the source directly".

But please note the comments under your question. While i just answered your question, i also consider it a bad idea to do it like that, because of the already mentioned reasons.

Sign up to request clarification or add additional context in comments.

2 Comments

I know Cart and CartTotalPrice should not be static properties but I am creating a test application. I have changed the declaration of my TextBlock as you described but the TextBlock is not refreshed automatically. For instance the TextBlock is not refreshed after the user adds an item to the cart.
Of course its not, thats why we all said its not a good idea. For it to be updated you would either need to implement INotifyPropertyChanged and raise every property change. But this won't work with static properties. The other option is to manually pull the changes via UpdateTarget.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.