Command Execute not reised in ItemContainerStyle

Topics: Prism v2 - Silverlight 2
Jun 9, 2009 at 2:15 PM
Edited Jun 9, 2009 at 3:37 PM


I am currently working in Silverlight using the CAL framework and I've been trying to create a region inside a TabControl whose items can be closed. The goal is to make use of Josh Smith's MVVM example to work with prism in silverlight. I created a "ClosableTabItemStyle" only to set the value for the HeaderTemplate and assigned that to the ItemContainerStyle. Then I created the DataTemplate itself with a TextBox to host my workspace name and a Button to host my CloseCommand. Everything worked just fine (including the binding to my viewmodels properties) except that the Command Execute was never raised. Then I wondered what will happen if I removed the command binding from the button and replace it with a simple Button.Click event handler. That didn't work either. I am verry puzzled at the moment and don't seem to find a workaround. Note that all resources are accessible through the Usercontrol.Resources with registered resource keys.

Can anyone please help.

Thanks in advance,


Jun 9, 2009 at 7:46 PM

Hi Constantinos,


Since the click event handler is failing to execute (and not the command), this does not appear to be a Prism issue. If your view is not very complex, you could start by rewriting it, always having a working button (add the button first, and as you add components check when it stops firing the event).


You might also want to post this question in the Silverlight forums. You could get a useful answer there.


I found these links in the Silverlight forums (it might not be related to your case, as I do not know your application):

·         Click on button doesn't work

·         Tip: How to handle the MouseLeftButtonDown and MouseLeftButtonUp events of the Button control?


Please let me know if this helps.


Damian Schenkelman

Jun 10, 2009 at 10:07 AM

Thanks for your reply Damian,

I have been searching in the suggested links for an answer to my problem but I had no luck yet. I posted here because I suspect that my problem is linked to prism throught the "regions:TabControlRegionAdater.ItemContainerStyle" attached property. Although, I posted in the Silverlight forums about it, I am copying the xaml needed to reproduce the problem. Just in case

Here is my silverlight forum post. 

The xaml code (Simplified without the command binding)

    Width="Auto" Height="Auto">

        <DataTemplate x:Key="TestMeDataTemplate">
            <Grid HorizontalAlignment="Stretch">
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="20" />
                <TextBlock Grid.Column="0" Margin="0,0,6,0"
                                        Text="{Binding Path=HeaderInfo.DisplayName}"
                <Button Grid.Column="1" Margin="3,0,0,0"
                                                Width="20" Height="20" Content="X" />
        <Style x:Key="TestMeStyle" TargetType="controls:TabItem">
            <Setter Property="HeaderTemplate" Value="{StaticResource TestMeDataTemplate}" />
    <Grid x:Name="LayoutRoot" Background="White" Width="Auto" Height="Auto">
            <controls:TabControl TabNavigation="Cycle" TabStripPlacement="Left"
                             regions:TabControlRegionAdapter.ItemContainerStyle ="{StaticResource TestMeStyle}"
                             Width="Auto" Height="Auto" />

Thanks for your help,


Jun 11, 2009 at 8:35 PM

Hi Constantinos,


I have reproduced your issue and found a workaround for it.



Add the click mode property to your button and set it to Press or Release value:

<Button Grid.Column="1" Margin="3,0,0,0" Click="Button_Click_1" VerticalAlignment="Center" ClickMode="Press" Width="20" Height="20" Content="X" />


Take into account, that another viable approach could be using a Tabcontrol whose Tabitems automatically have the “x” button to close them. There should be a lot of examples of this on the Web or you could implement your own.

This is because, even if the button does fire the click event, you might have trouble knowing which view does the TabItem with the button that was click belongs to, and thus unable to know which view to remove from the region.


Please let me know if this helps.


Damian Schenkelman

Jun 11, 2009 at 9:50 PM
Edited Jun 11, 2009 at 9:51 PM

Thank you so much for posting this fix... I've been watching this thread like a hawk because I was having the same issue. This fixed it for me.


With regards to the issue of how to determine the view that is to be removed, I let each view presenter handle it's own view removal with a locally available command:



In your HeaderTemplate:

<Button x:Name="btnClose" Commands:Click.Command="{Binding CloseCommand}" ClickMode="Press">


In the constructor:

CloseCommand = new DelegateCommand<string>(OnCloseExecute, OnCloseCanExecute);

The command:

        public DelegateCommand<string> CloseCommand { get; private set; }

        void OnCloseExecute(string ViewName)


            IRegion contentregion = m_regionmanager.Regions["ContentRegion"];



        bool OnCloseCanExecute(string ViewName)


            return true;


I am not using the ViewName being passed in - it's to satisfy the delegate type requirement.

Also, View is my presenter's property which holds... you guessed it... the view.



Jun 12, 2009 at 9:25 AM

Demian you saved the day. It worked like a charm for me too.

I was starting to give up on this, since noone responded in the Silverlight forums.

Thank you so much

Prism rules!