Hussein
Hussein

Reputation: 989

TabControl SelectedItem Binding Problems

In a Tabbed interface application i'm trying to pass the TabItem Header string or the object contained in the selected TabItem to the view model to use it to publish an event like this:

In the View (xaml):

<TabControl  x:Name="MyTC"
                         prism:RegionManager.RegionName="{x:Static inf:RegionNames.MainRegion}"
                         SelectedItem="{Binding Path=TabControlSelectedItem,UpdateSourceTrigger=PropertyChanged,Mode=Twoway}"
                         Cursor="Hand"
                         Grid.Row="0" 
                         Grid.Column="1">


                <TabControl.ItemTemplate>

                    <DataTemplate>
                        <!--DataContext="{Binding ElementName=MyTC, Path=SelectedItem}"-->
                        <StackPanel  Orientation="Horizontal">

                            <TextBlock VerticalAlignment="Center"
                                       Margin="3"
                                       Text="{Binding Path=DataContext.DataContext.HeaderInfo, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}"
                                        />



                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="MouseLeftButtonDown">
                                    <i:InvokeCommandAction Command="{Binding HeaderClickCommand}"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>


                        </StackPanel>

                    </DataTemplate>

                </TabControl.ItemTemplate>


            </TabControl>

In View Model

//***********************************************************

     //Constructor


public ShellWindowViewModel(IEventAggregator eventAggregator)
    {
        this.eventAggregator = eventAggregator;
        this.HeaderClickCommand = new DelegateCommand(OnHeaderClick);
    }


        //SelectedItem Binding 
        private object tabControlSelectedItem;
        public object TabControlSelectedItem
        {
            get { return tabControlSelectedItem; }
            set
            {
                if (tabControlSelectedItem != value)
                {
                    tabControlSelectedItem = value;
                    OnPropertyChanged("TabControlSelectedItem");


                }
            }
        }

        //*****************************************************************************************************

        //this handler publish the Payload "SelectedSubsystem" for whoever subscribe to this event
        private void OnHeaderClick()
        {
            //EA for communication between Modules not within Modules
            string TabHeader = (TabControlSelectedItem as TabItem).Header.ToString();
            eventAggregator.GetEvent<SubsystemIDSelectedEvent>().Publish(TabHeader);

        }

but there is something wrong because when i click the TabItem nothing happen and when i inserted a breakpoint @ TabControlSelectedItem property i found it contain the view namespace.i want the TabControlSelectedItem to get the selected Tab header string or the object in the selected tab item. Your help is very much appreciated.

Upvotes: 1

Views: 9601

Answers (1)

user1029697
user1029697

Reputation: 150

I just tried something similar and it works fine.

Here is my xaml

<TabControl ItemsSource="{Binding Matches}"
    SelectedItem="{Binding SelectedRecipe}">
  <TabControl.ItemTemplate>
    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0"
                    Text="{Binding Path=Date}" />
                 <TextBlock Grid.Column="1"
                    TextAlignment="Center"
             Text="{Binding Path=Name}" />
        </Grid>
    </DataTemplate>
  </TabControl.ItemTemplate>
</TabControl>

And then in my viewmodel

public object SelectedRecipe
{
  get
  {
    return _selectedRecipe;
  }
  set
  {
    _selectedRecipe = value;
    OnNotifyPropertyChanged("SelectedRecipe");
  }
}

Upvotes: 0

Related Questions