wpf 根据条件显示/隐藏 itemscontrol 中的项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18299191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show/Hide items in itemscontrol on condition
提问by pgwri
I have an Itemscontrol that I am using to display a list of start information sent from a timing system.
我有一个 Itemscontrol,我用它来显示从计时系统发送的启动信息列表。
I need to be able to "switch off"/stop displaying a set of lane info if the lane number isn't showing (if it's null or a blank) and then if the timer sends info to switch the lane back on then show the data again.
如果车道号码未显示(如果它为空或空白),然后如果计时器发送信息以重新打开车道,则我需要能够“关闭”/停止显示一组车道信息再次数据。
I can't set it to just delete everything because the timer sends its information constantly and everything except the lane number would reappear again.
我不能将它设置为只删除所有内容,因为计时器不断发送其信息,并且除了车道号码之外的所有内容都会再次出现。
Is it possible to show/hide items on condition?
是否可以在条件下显示/隐藏项目?
What is currently happening
目前正在发生什么
Lanes
1 ------
2 ------
------ <- other info remains
4 ------
What I want to happen
我想要发生的事情
Lanes
1 ------
2 ------
4 ------
Here is a sample of my itemscontrol code
这是我的 itemscontrol 代码示例
<ItemsControl ItemsSource="{Binding CHeat.SwimList}" Margin="10,0" HorizontalAlignment="Left" VerticalAlignment="Top">
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Lane" />
<Label Content="Pos" />
<Label Content="Swimmer" />
<Label Content="Club" />
<Label Content="Time" />
</StackPanel>
<ItemsPresenter/>
</StackPanel>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="35" />
<ColumnDefinition Width="30" />
<ColumnDefinition Width="150" />
<ColumnDefinition Width="50" />
<ColumnDefinition Width="80" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="{Binding LaneNumber}" />
<Label Grid.Column="1" Content="{Binding Position}" />
<Label Grid.Column="2" Content="{Binding Swimmer}" />
<Label Grid.Column="3" Content="{Binding Club}" />
<Label Grid.Column="4" Content="{Binding Time}" />
</Grid>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
If there was a way I could set Content = ""for the other data based off the LaneNumber then I believe it could work, because then I can just bring back the bindings.
如果有一种方法可以Content = ""根据 LaneNumber设置其他数据,那么我相信它可以工作,因为这样我就可以恢复绑定。
I'm fairly new to WPF so extra detail would be helpful many thanks!
我对 WPF 还很陌生,所以额外的细节会很有帮助,非常感谢!
采纳答案by Nadeem_MK
Why don't you try using the Visibilityproperty?
you just have to create a public property in your MVVM or code behind and the Bind it to the element you want to hide.
你为什么不尝试使用该Visibility属性?您只需要在 MVVM 或后面的代码中创建一个公共属性,然后将其绑定到要隐藏的元素。
<StackPanel Visibility="{Binding ShowElement, Converter={StaticResource VisibilityConverter}, Mode=TwoWay}">
By setting the boolean value of ShowElement, you can easily hide or show the StackPanel.
通过设置 ShowElement 的布尔值,您可以轻松隐藏或显示 StackPanel。

