3

In the MVVM demo app by Josh Smith, When you attempt to close a tab via the close button on that tab (which is not the currently selected tab), then it will not close, instead it closes the selected tab.

How can I amend the code so that when I click on the close button for a tab item that not the currently selected one, it closes the right tab?

Note, I have tried using AttachedProperties and the following:

AttachedCommand:CommandBehavior.Event="Click"
AttachedCommand:CommandBehavior.Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.CloseWorkspaceCommand}"
AttachedCommand:CommandBehavior.CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext}"

where I pass in the CommandParameter, but this gets the ViewModel of the hosting View and not the View Model bound to the ContentPresenter.

Thanks for your time.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
  • 2
    post some strip-down.simplified example:View/ViewModel. Seems like a simple thing... without your code, it's hard to tell where your are stuck. There are too many possibilities. I can code up something as an answer, but that's a waste of time -- might look good but wouldn't help you any, which is the bottom line (besides the points)! – denis morozov Jan 05 '14 at 01:21
  • 1
    have you tried just {Bindnig} ? – ZSH Jan 05 '14 at 07:24
  • @ZSH, spot on mate. That's the answer. If you post this as an answer *with* _why this works_, I will gladly accept! Thanks very much for your time! – MoonKnight Jan 05 '14 at 10:56
  • @denismorozov Thanks very much for your time. I didn't post the full code due to its length and thought those familiar with Josh Smiths example MVVM app would know what I am talking about (most MVVM WPF guys have used this example). Thanks again! – MoonKnight Jan 05 '14 at 10:57

1 Answers1

1

Try just using this , it will bind directly to the datacontext of the view

AttachedCommand:CommandBehavior.CommandParameter="{Binding}"

I hope this helps.

ZSH
  • 905
  • 5
  • 15
  • I edited this to make it a bit clearer, I hope you don't mind :]. Can you also add _why_ this works, as this is not clear to me... Thanks again for your time. – MoonKnight Jan 05 '14 at 11:42