1

在选项卡式界面应用程序中,我试图将 TabItem Header 字符串或包含在所选 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 属性时,我发现它包含视图命名空间。我希望 TabControlSelectedItem 获取选定的选项卡标题字符串或选定选项卡项中的对象。非常感激你的帮助。

4

1 回答 1

0

我刚刚尝试了类似的东西,它工作正常。

这是我的xml

<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>

然后在我的视图模型中

public object SelectedRecipe
{
  get
  {
    return _selectedRecipe;
  }
  set
  {
    _selectedRecipe = value;
    OnNotifyPropertyChanged("SelectedRecipe");
  }
}
于 2013-02-28T15:59:03.107 回答