在WPF中绑定TabItem [英] Binding a TabItem in WPF

查看:73
本文介绍了在WPF中绑定TabItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中,按钮可以绑定到命令.

In WPF, a button can be bound to a command.

<Button Command="{Binding DoSomething}">Click me!</Button>

现在我想对TabItem做同样的事情:

Now I want to do the same to a TabItem:

<TabItem Header="A little tab" ???="{Binding DoSomething}">...</TabItem>

??? 应该是什么?还是有另一种方法?

What should ??? be? Or is there another way to do it?

推荐答案

这取决于您要实现的目标.TabItem具有IsSelected属性

It depends on what do you want to achieve. TabItems have the IsSelected property

IsSelected="{Binding IsSelected}"

可以被TwoWay限制,并且可以用来向ViewModel发出信号.

that can be bounded TwoWay, and can be used to signal stuff to the ViewModel.

您还可以使用以下事实:可以使用交互性覆盖TabItem的标题,并将其绑定到命令.

You could also use the fact that you can override the header of the TabItem, and bound it to a command, using Interactivity.

 <TabItem TabIndex="0"
                 Tag="{Binding CurrentPrinterStatus}">
            <TabItem.Header>
                <Grid Background="Transparent">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDown">
                            <i:InvokeCommandAction Command="{Binding DoSomething}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <TextBlock Style="{StaticResource TextBlockSelectedStyle}"
                           Text="Printers"/>
                </Grid>
            </TabItem.Header>

其他解决方案是使用TabControl的SelectionChanged事件,它可以使您找到当前选定的TabItem的ViewModel.

Other solutions are to use the SelectionChanged event of the TabControl, and that could allow you to find the ViewModel of the TabItem currently selected.

希望这些想法至少可以帮助您解决问题.

Hope this ideas atleast help you get a solution to your problem.

这篇关于在WPF中绑定TabItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆