Click here to Skip to main content
       

Silverlight / WPF

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionWPF Tab Item Close Button [modified]memberKevin Marois29 Jan '13 - 18:03 
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:12.

AnswerRe: WPF Tab Item Close Button PinmemberSledgeHammer0129 Jan '13 - 19:02 
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}}
GeneralRe: WPF Tab Item Close Button PinmemberKevin Marois2 Feb '13 - 15:33 
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

GeneralRe: WPF Tab Item Close Button PinmemberSledgeHammer012 Feb '13 - 18:25 
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?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 20 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid