0

I want to bind to the Items.IndexOf(tbXy).

In c# it's easy to access the IndexOf:

 tbControl.Items.IndexOf(tbXy)

but in XAML? I tried this:

 Value="{Binding Path=Items.IndexOf(tbxY)}"

I also found this suggestions with the help of converters:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/139bed8b-8eb0-4aec-a9c0-521bd7bede91/determining-index-of-a-tabitem?forum=wpf

Any suggestions to do this simple directly binding?

benba
  • 821
  • 9
  • 16
  • you can't do that. What do you want that for? – Federico Berasategui Aug 07 '14 at 15:56
  • 1
    You can't bind to a method by default, however there [are](http://stackoverflow.com/q/502250/302677) [workarounds](http://stackoverflow.com/q/19980612/302677) if you really need it. That said, what are you trying to do? There is probably a much better way of accomplishing the task using WPF's binding system. – Rachel Aug 07 '14 at 16:03
  • @HighCore: I have a TabControl with one TabItem which should be visible when a button is pressed (done in XAML) and get focus (isSelected). Instead of using the TabIndex (e.g. 3) directly, it should be independent as possible. – benba Aug 08 '14 at 21:31
  • @benba then assign an `x:Name` to the `TabItem` and use ElementName bindings. I have no idea what you're talking about if you don't post the relevant code and XAML. – Federico Berasategui Aug 08 '14 at 21:33

1 Answers1

0

Wouldn't it make more sense to push that logic to the ViewModel? If tbxY is an item that is known in code-behind, couldn't you just create a property on the ViewModel called "SelectedItem" and then bind elsewhere?

You can do some pretty spiffy things with DataTemplates that would really let you do whatever you want with the actual item, once you're bound to it.

Locke
  • 1,133
  • 11
  • 32
  • My goal is to have as little as possible additional code in the ViewModel. Furthermore the "x:Name" of this TabItem is View specific and the ViewModel shouldn't know anything about the View... – benba Aug 08 '14 at 22:24
  • Perhaps I'm missing something, but... why are you needing to bind to the x:Name -- that should be irrelevant. Yes, it will be slightly ugly, if you are binding to a TabItem, rather than the class that should be BOUND to the TabItem, but simply making the DataBinding an object on the ViewModel will let you get what you want. You should not be afraid to put extra code in a viewmodel. That's the purpose of a ViewModel. – Locke Aug 13 '14 at 14:38