|
I created a tab stle so that it has a close button. The button is bound to a command:
<Style TargetType="{x:Type TabItem}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Close"
Command="{Binding CloseTabCommand}"/>
<MenuItem Header="Close All But This"
Command="{Binding CloseAllOtherTabsCommand}"/>
<MenuItem Header="Float"
Command="{Binding FloatTabCommand}"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
I tried this:
<MenuItem Header="Close"
Command="{Binding CloseTabCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}"/>
but I get null passed to the command.
I need to pass along a reference to the tab. Can someone show me how to get the tab in this style and pass it as a parameter?
Thank you
If it's not broken, fix it until it is
modified 30-Jan-13 0:12am.
|
|
|
|
|
You won't find the TabItem like that because the ContextMenu is a pop up window and thus is in a different visual tree.
Do this instead:
{Binding Path=PlacementTarget, RelativeSource={RelativeSource
AncestorType=ContextMenu}}
|
|
|
|
|
Ok, I finally got around to this again. So now I'm trying this:
<Style TargetType="{x:Type TabItem}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Close"
Command="{Binding CloseTabCommand}"
CommandParameter="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
</ContextMenu>
</Setter.Value>
</Setter>
Here's my command:
private ICommand _CloseTabCommand;
public ICommand CloseTabCommand
{
get
{
if (_CloseTabCommand == null)
_CloseTabCommand = new RelayCommand<object>(p => closeTabExecuted(p), p => closeTabCanExecute(p));
return _CloseTabCommand;
}
}
and the methods:
private bool closeTabCanExecute(object obj)
{
return true;
}
private void closeTabExecuted(object obj)
{
TabManager.CloseActiveTab();
}
Again, the command fires but null is passed to closeTabExecuted.
I'm stuck here. Could use some more help.
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
Works all day long for me. I copy & pasted your code and got the TabItem in both closeTabExecuted and closeTabCanExecute. Are you sure your RelayCommandT is not broken?
|
|
|
|