开发者

how to get TabHeader on trigger

i have a tabcontrol. i'm trying to pass the tabcontrol as a parameter to figure out the selected tab item so i can get the tab header name. Binding this doesn't seem to work. ideas?

<TabControl Background="#FFF9F9F9" Height="650">
    <i:Interaction.Triggers>
        <i:EventTrigger  EventName="SelectionChanged">
            <n:ExecuteCommandAction Command="{Binding UpdateTabCommand}" Parameter="{Binding this}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>

in my viewmodel constructor i have:

_updateTabCommand 开发者_C百科= new ActionCommand< TabControl>(UpdateTab);

private method:

public void UpdateTab(TabControl tabControl)
{
    var tabItem = (TabItem)tabControl.SelectedItem;


1) Use ElementName binding.

Example:

<TabControl Background="#FFF9F9F9"
            Height="650"
            Name="TabControl1">
    <i:Interaction.Triggers>
        <i:EventTrigger  EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding UpdateTabCommand}"
                                    CommandParameter="{Binding ElementName=TabControl1}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TabControl>

2) Use FindAncestor binding:

Example:

<i:Interaction.Triggers>
    <i:EventTrigger  EventName="SelectionChanged">
        <i:InvokeCommandAction Command="{Binding UpdateTabCommand}"
                                CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TabControl}}" />
    </i:EventTrigger>
</i:Interaction.Triggers>


First of all there is no such thing as {Binding this} in WPF. If you want to refer to the element on which you are setting Binding use RelativeSource binding with mode set to Self.

Second observation. Passing UI elements to ViewModel smells badly (impacts testability, increases code coupling and most likely the class will end up violating more good design principles). The fix is really simple: just bind TabControl.SelectedItem to the field on ViewModel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜