基于布尔值的 ListView 项目可见性
Posted
技术标签:
【中文标题】基于布尔值的 ListView 项目可见性【英文标题】:ListView item visibility based on boolean 【发布时间】:2017-06-23 10:59:59 【问题描述】:我正在尝试根据输入文本过滤 ListView 项目,并且由于选择被删除,我正在尝试根据布尔值将可见性设置为不可见(我希望这不会删除项目的选择)
我使用了一些较早的帖子来创建此代码,但我无法弄清楚它为什么不起作用:
<ListView x:Name="osebe_listView" Margin="0,0,40,0" SelectionMode="Multiple" SelectionChanged="osebe_listView_SelectionChanged" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
<ListView.Resources>
<Style TargetType="x:Type ListViewItem">
<Style.Triggers>
<DataTrigger Binding="Binding visible" Value="False">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Style TargetType="x:Type GridViewColumnHeader">
<Setter Property="Background" Value="#1AFFFFFF" />
<Setter Property="BorderThickness" Value="0"/>
</Style>
<Style TargetType="x:Type ColumnDefinition">
<Setter Property="Width" Value="10"/>
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="Naziv podjetja" DisplayMemberBinding="Binding nazivPodjetja"/>
<GridViewColumn Header="Ime" DisplayMemberBinding="Binding ime" Width="25"/>
<GridViewColumn Header="Priimek" DisplayMemberBinding="Binding priimek" Width="Binding ActualWidth, ElementName=helperField"/>
<GridViewColumn Header="Naslov" DisplayMemberBinding="Binding naslov" Width="Binding ActualWidth, ElementName=helperField"/>
<GridViewColumn Header="Pošta" DisplayMemberBinding="Binding posta" Width="Binding ActualWidth, ElementName=helperField"/>
<GridViewColumn Header="Mobilna številka" DisplayMemberBinding="Binding mobilnaSt" Width="Binding ActualWidth, ElementName=helperField"/>
<GridViewColumn Header="Stacionarna številka" DisplayMemberBinding="Binding stacionarnaSt" Width="Binding ActualWidth, ElementName=helperField"/>
<GridViewColumn Header="E-naslov" DisplayMemberBinding="Binding eMail" Width="Binding ActualWidth, ElementName=helperField"/>
<GridViewColumn Header="Skupina" DisplayMemberBinding="Binding skupina" Width="Binding ActualWidth, ElementName=helperField"/>
</GridView>
</ListView.View>
</ListView>
文本过滤方法(效果很好):
private void isci_tb_TextChanged(object sender, TextChangedEventArgs e)
try
if (isci_tb.Text == "search") return;
string s = isci_tb.Text;
if (isci_tb.Text == "")
foreach (Oseba o in seznamOseb)
//osebe_listView.Items.Add(o);
o.visible = true;
//else
//
// osebe_listView.Items.Clear();
foreach (Oseba o in seznamOseb)
if(! (contejns(o.ime, s) || contejns(o.priimek, s) || contejns(o.mobilnaSt.ToString(), s) || contejns(o.posta, s) || contejns(o.nazivPodjetja, s) || contejns(o.stacionarnaSt.ToString(), s) || contejns(o.naslov, s) || contejns(o.eMail, s)))
o.visible = false;
//
catch
我找到了code here
请注意,我没有使用任何数据绑定。我错过了什么?
【问题讨论】:
Oseba 类是否实现了 INotifyPropertyChanged 接口? 在他使用<Setter Property="Visibility" Value="Collapsed"/>
的链接中,在您的代码中找不到。 o.visible
什么也没做,我猜..
它没有。我该如何实施?我认为这样设置属性就足够了,因为我在那篇文章中没有找到任何额外的代码
【参考方案1】:
确保Oseba
类实现INotifyPropertyChanged
接口并在visible
属性的设置器中引发PropertyChanged
事件:
public class Oseba : INotifyPropertyChanged
private bool _isVisible;
public bool visible
get return _isVisible;
set _isVisible = value; NotifyPropertyChanged("visible");
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
这是动态触发ListViewItem
样式的数据触发器所必需的。
您还应该使用DataTrigger
将ListView
的ItemContainerStyle
属性设置为您的自定义样式:
<ListView x:Name="osebe_listView" Margin="0,0,40,0" SelectionMode="Multiple" SelectionChanged="osebe_listView_SelectionChanged" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
<ListView.ItemContainerStyle>
<Style TargetType="x:Type ListViewItem">
<Style.Triggers>
<DataTrigger Binding="Binding visible" Value="False">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
<ListView.Resources>
<Style TargetType="x:Type GridViewColumnHeader">
<Setter Property="Background" Value="#1AFFFFFF" />
<Setter Property="BorderThickness" Value="0"/>
</Style>
<Style TargetType="x:Type ColumnDefinition">
<Setter Property="Width" Value="10"/>
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="Naziv podjetja" DisplayMemberBinding="Binding nazivPodjetja"/>
<GridViewColumn Header="Ime" DisplayMemberBinding="Binding ime" Width="25"/>
<GridViewColumn Header="Priimek" DisplayMemberBinding="Binding priimek" Width="Binding ActualWidth, ElementName=helperField"/>
<GridViewColumn Header="Naslov" DisplayMemberBinding="Binding naslov" Width="Binding ActualWidth, ElementName=helperField"/>
<GridViewColumn Header="Pošta" DisplayMemberBinding="Binding posta" Width="Binding ActualWidth, ElementName=helperField"/>
<GridViewColumn Header="Mobilna številka" DisplayMemberBinding="Binding mobilnaSt" Width="Binding ActualWidth, ElementName=helperField"/>
<GridViewColumn Header="Stacionarna številka" DisplayMemberBinding="Binding stacionarnaSt" Width="Binding ActualWidth, ElementName=helperField"/>
<GridViewColumn Header="E-naslov" DisplayMemberBinding="Binding eMail" Width="Binding ActualWidth, ElementName=helperField"/>
<GridViewColumn Header="Skupina" DisplayMemberBinding="Binding skupina" Width="Binding ActualWidth, ElementName=helperField"/>
</GridView>
</ListView.View>
</ListView>
【讨论】:
成功了,非常感谢!它也不会像我希望的那样删除选择。为什么需要这个接口?据我了解,该属性会触发事件或类似事件?对不起,我知识贫乏,我才一年 C# :) 在绑定客户端和数据源之间的绑定中需要更改通知,如 MSDN 上的文档所述:msdn.microsoft.com/en-us/library/… 好的会调查的。我做到了,我猜,一旦我达到代表的门槛,它就会显示出来。 :)以上是关于基于布尔值的 ListView 项目可见性的主要内容,如果未能解决你的问题,请参考以下文章
在具有布尔值的 KnockoutJS 中显示隐藏 div 并应用 css 规则