WPF 绑定列表框和 TabItem
WPF 新手,我正在尝试做一些基本的事情(我认为!)。我有一个 TabControl 和一个 ListBox,显示打开的选项卡项:
<ListBox Width="170" Height="188" ItemsSource="{Binding Items, ElementName=tabControl}" Name="ListTabs" Canvas.Left="0" Canvas.Top="27">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
El
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Header}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
是否可以绑定到特定的选项卡项(tabitem2 和 tabitem3)而不是整个选项卡控件?原因是第一个 tabitem1 是欢迎选项卡,我不希望它显示在列表框中。
更新:
有人愿意发布一些关于如何使用 IValueConverter 隐藏/过滤 tabitem 的代码吗?我已经搜索了几个小时但没有运气。非常非常感谢!
New to WPF, am trying to do something basic (I think!). I have a TabControl and a ListBox that shows what tabitems are open:
<ListBox Width="170" Height="188" ItemsSource="{Binding Items, ElementName=tabControl}" Name="ListTabs" Canvas.Left="0" Canvas.Top="27">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
El
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Header}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Is it possible to bind to specific tabitems (tabitem2 and tabitem3) rather than the whole tabcontrol? Reason being is the first tabitem1 is a welcome tab and I don't want it to be shown in the listbox.
UPDATE:
Would someone be so kind to post some code on how to use an IValueConverter to hide/filter a tabitem? I have been searching for hours with no luck. Many many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在您当前的设置中,唯一的方法是通过 IValueConverter。
如果您愿意修改您的方法,您可以将
ListBox.ItemsSource
绑定到 ICollectionView,然后使用Filter
属性。In your current set up the only way would be to run it through an IValueConverter.
If you were willing to modify your approach you could bind the
ListBox.ItemsSource
to a ICollectionView and then make use of theFilter
property.您可以向 ItemSource 添加转换器,然后在转换器中删除欢迎页面或进行任何您想要的更改。
you can add a converter to ItemSource and then in the converter remove the welcome page or do any changes that you want .
我建议不要这样做。对 Listbox 和 Tabcontrol 使用通用数据源。
要过滤/拦截任何数据绑定,您可以使用 IValueConverter。
I recommend not doing this. Use a common data source instead with both Listbox and Tabcontrol.
To filter/intercept any data binding, you can use IValueConverter.
您必须过滤掉欢迎选项卡,因此您需要在 CollectionView 您可以绑定到 CollectionView,而不是绑定到选项卡控件的项目。
尽管 ValueConverter 可能有效,但我认为这是一种黑客行为。
You would have to filter out the welcome tab so you will need to add a Filter on a CollectionView Instead of binding to the items of the tab control you'd bind to the collectionview.
Although a ValueConverter might work, I consider that a kind of hack.