Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF MVVM
Hello, all
 
i'm using WPF and MVVM pattern and I try to create closeable tabitem control. But my binded method CloseCommand on the button in template of the tabitem doesn't work. And works on any button in the window. I dont understand, why? Below code of the test project.
 
Thanks.
 
ViewModel
namespace CloseableTabControl.ViewModel
{
    public class CloseableTabControlVM
    {
        private RelayCommand _closeCommand;
        public ICommand CloseCommand
        {
            get {
                if (_closeCommand == null )
                _closeCommand = new RelayCommand(param => this.closeButton_Execute());
 
                return _closeCommand;
            }
        }
        
        private ObservableCollection<IPageBase> _pages;
 
        public ObservableCollection<IPageBase> Pages
        {
            get { return _pages; }
            set { _pages = value; }
        }
 

        public CloseableTabControlVM()
        {
            _pages = new ObservableCollection<IPageBase>();
            _pages.Add(new TabItem1VM());
            _pages.Add(new TabItem2VM());
            _pages.Add(new TabItem1VM());
        }
 
        private bool closeButton_canExecute() { return true; }
 
        private void closeButton_Execute() 
        {
            MessageBox.Show("Click me!");
        }
    }
}
 
Commands
namespace CloseableTabControl.Command
{
    public class RelayCommand : ICommand
    {
 
        #region Fields
        private readonly Predicate<object> _canExecute;
        private readonly Action<object> _execute;
        #endregion
 
        #region Constructor
        public RelayCommand(Action<object> execute):this(null, execute) { }
 
        public RelayCommand(Predicate<object> canExecute, Action<object> execute)
        {
            if (execute == null) throw new ArgumentNullException("execute");
            _execute = execute;
            _canExecute = canExecute;
        }
        #endregion
 
        [DebuggerStepThrough]
        public bool CanExecute(object param)
        {
            return _canExecute == null ? true : _canExecute(param); 
        }
 
        public void Execute(object param)
        {
            _execute(param);
        }
 
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }
}
 
MainWindow Code
namespace CloseableTabControl
{
    public partial class MainWindow : Window
    {
        private CloseableTabControlVM _closeTab;
        public CloseableTabControlVM CloseTab
        {
            get { return _closeTab; }
            set { _closeTab = value; }
        }
 
        public MainWindow()
        {
            _closeTab = new CloseableTabControlVM();
            InitializeComponent();
        }
    }
}
 
And MainVindow XAML
<Window x:Class="CloseableTabControl.MainWindow"
        x:Name="MainWindowInstance"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:v="clr-namespace:CloseableTabControl.View"
        xmlns:vm="clr-namespace:CloseableTabControl.ViewModel"
        DataContext="{Binding CloseTab,ElementName=MainWindowInstance}"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate DataType="{x:Type vm:TabItem1VM}">
            <v:TabItem1/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:TabItem2VM}">
            <v:TabItem2/>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <TabControl 
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Margin="10,10,10,10"
            Width="500"
            Height="300"
            ItemsSource="{Binding Pages}">
            <TabControl.ItemContainerStyle>
                <Style TargetType="TabItem">
                    <Setter Property="HeaderTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Header}"/>
                                    <Button Content="x" Margin="0" Command="{Binding CloseCommand}" Width="16" Height="16"/>
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TabControl.ItemContainerStyle>
        </TabControl>
    </Grid>
</Window>
Posted 31-Jan-13 3:47am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If you look in your Output window when debugging, you'll see that you have binding errors in there relating to the CloseCommand, stating that it doesn't belong to the relevant VM. That's because your command isn't defined at the VM level - rather, it's defined at the container level. To fix this, use the relative source to find the tab item that contains this - and bind to that DataContext, like this:
<Button Content="x" Margin="0" Command="{Binding DataContext.CloseCommand, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}" Width="16" Height="16"/>
  Permalink  
Comments
Andrey A. Eroshenko at 31-Jan-13 10:55am
   
Thank you. It helped
Sergey Alexandrovich Kryukov at 31-Jan-13 11:10am
   
Sure, a 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 359
1 Maciej Los 290
2 Sergey Alexandrovich Kryukov 230
3 Shweta N Mishra 185
4 PIEBALDconsult 149
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,760


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100