带参数的Wpf tabcontrol trig

带参数的Wpf tabcontrol trig,wpf,tabcontrol,Wpf,Tabcontrol,我在wpf中有一个tabcontrol,我想在选项卡选择更改时向我的viewmodel发送一个触发器。触发器应该包括选项卡名称或选项卡索引 <i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <i:InvokeCommandAction Command="{Binding Path=TabChangedCommand}"

我在wpf中有一个tabcontrol,我想在选项卡选择更改时向我的viewmodel发送一个触发器。触发器应该包括选项卡名称或选项卡索引

<i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
        <i:InvokeCommandAction Command="{Binding Path=TabChangedCommand}"
                               CommandParameter="{Binding ElementName=TabControl, Path=Name}"/>
        </i:EventTrigger>
</i:Interaction.Triggers>


触发器工作正常,但参数始终为null。

您没有在CommandParameter中传递ElementName

    <TabControl x:Name="mytab">
     ....
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding Path=TabChangedCommand}"
                                   CommandParameter="{Binding ElementName=mytab}"/>
            </i:EventTrigger>
    </i:Interaction.Triggers>
    ....
    </TabControl>

....
....

做这样的事情

您可以通过使用MVVM实现您的目标

ViewModel中有两个属性,一个属性用于保存所有可用选项卡的集合,另一个属性用于保存当前选定的选项卡

视图模型

public ObservableCollection<ITabViewModel> Tabs { get; private set; }

public ITabViewModel SelectedTab
{
    get { return _selectedTab; }
    set
    {
        _selectedTab = value;
        RaisePropertyChanged(() => SelectedTab);
    }
}
public int SelectedTabIndex
{
    get { return _selectedTabIndex; }
    set
    {
        _selectedTabIndex = value;
        RaisePropertyChanged(() => SelectedTabIndex);
    }
}
XAML

<TabControl ItemsSource="{Binding Tabs}"
            SelectedItem="{Binding SelectedTab}" />
<TabControl ItemsSource="{Binding Tabs}"
            SelectedIndex="{Binding SelectedTabIndex}"  />


共享TabControlPost XAML的代码,以获得完整的TabControl和ViewModel。