TabControl SelectedItem绑定问题

时间:2013-02-25 10:15:19

标签: wpf mvvm prism

在选项卡式界面应用程序中,我试图将TabItem标题字符串或包含在选定TabItem中的对象传递给视图模型以使用它 发布这样的事件:

在视图(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>

在视图模型中

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

     //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);

        }

但有一些错误,因为当我点击TabItem时没有任何事情发生,当我插入断点时@ TabControlSelectedItem 属性我发现它包含视图namespace.i希望TabControlSelectedItem获取选定的Tab标题字符串或对象in 选定的标签项。 非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我刚试过类似的东西,但效果很好。

这是我的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>

然后在我的viewmodel中

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