WPF Reflections

Dec 6 2007   3:42PM GMT

Current item in binding



Posted by: Mark Shurmer
WPF

The question i was faced with was how to bind to a current item in an array. Why? I wanted to display the result of the ToString() method on the whole object - it should display something like ‘buy 100 abc.o @ 50′

I had an array (of Equities, but it could have been anything), and some xaml binding to it:

<StackPanel DataContext={x:Static EquitiesModel.Equities}>
<TextBlock Text={Binding} />
<ListBox ItemsSource={Binding}IsSynchronizedWithCurrentItem=True />
</StackPanel>

That didn’t work because the {Binding} binds to the whole array, and i just needed the current item. It turned out to be some syntax that I hadn’t seen since XPath:

<StackPanel DataContext={x:Static EquitiesModel.Equities}>
<TextBlock Text={Binding /} />
<ListBox ItemsSource={Binding}IsSynchronizedWithCurrentItem=True />
</StackPanel>

Lo and behold, it used the ToString() method, and I was happy!

Comment on this Post


You must be logged-in to post a comment. Log-in/Register