views:

926

answers:

3

Im Using Composite Aplication Guidiance Pattern for building my WPF application. In my Shell i have a tabcontrol wich contains a region for dynamicly load Views into the region. The views is loaded into new tabs in the TabControl.

  <TabControl     
      AutomationProperties.AutomationId="MainTabControl" 
      cal:RegionManager.RegionName="{x:Static inf:RegionNames.MainRegion}" 
      Width="Auto" Height="Auto" Margin="10,10,0,0" 
      HorizontalAlignment="Stretch"                            
      IsSynchronizedWithCurrentItem="True"
      ItemTemplate="{StaticResource TabItemTemplate}"
SelectionChanged="TabControl_SelectionChanged">

I have a DataTemplate "TabItemTemplate" for implementing a CloseButton. I cant figure out how to bind the button´s command in the DataTemplate to the Close Command in the presentationModel. If I bind the command to a CompositCommand, the command is executet. But then I must figure out wich tab that close button was pressed and only execute closeCommand in that PresentationModel.. Below is the dataTemplate.

<DataTemplate x:Key="ClosableTabItemTemplate">
            <DockPanel Width="120">
                <Button 
                    Command="inf:CloseCommands.CloseCommand"
                    Content="X"
                    Cursor="Hand"
                    DockPanel.Dock="Right"
                    Focusable="False"
                    FontFamily="Courier" 
                    FontSize="9"
                    FontWeight="Bold"  
                    Margin="0,1,0,0"
                    Padding="0"
                    VerticalContentAlignment="Bottom"
                    Width="16" Height="16" 
                    />
                <ContentPresenter 
        Content="{Binding}" 
        VerticalAlignment="Center" 
        />
            </DockPanel>
        </DataTemplate>

Does anyone know how to solve this binding problem??

A: 

You should either bind to a command instance on your viewmodel, such as a DelegateCommand exposed by a property, or bind the CommandParameter to the DataContext of the TabItem so that a shared command can be passed the item.

Denis Troller
I already tried to bind to a DelegateCommand, it doesnt seems to Work. I seems like the DataTemplate does not know the DataContext of the TabItem.
KaJo
How do you add your view into the region ? (what does your code look like?)
Denis Troller
A: 

I´ve found a solution to this problem. The problem was when i bind a UserControl to a TabControl, it´s only the contentpane´s datacontext that is set to the usercontrol and the datacontext for the headerpane is still null. But if I define two datatemplates, one for the item and one for the content, and then add the presentationModel to the region, then dataContext for both item and content is populated. I can then in itemTemplate bind to a delegateCommand Property in the presentationModel.

KaJo
A: 

Could you please post an example for that. I´m working on it right now and can´t figure it out. Thank you