我正在尝试根据输入文本过滤ListView项目,并且因为选择被删除,我试图基于布尔值将visibilty设置为不可见(我希望这不会删除项目的选择)
我使用了一些较旧的帖子来创建此代码,但我无法弄清楚它为什么不起作用:
<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
请注意,我没有使用任何数据绑定。我错过了什么?
答案 0 :(得分:1)
确保Oseba
类实现INotifyPropertyChanged
接口,并在PropertyChanged
属性的setter中引发visible
事件:
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
样式中数据触发器动态触发所必需的。
您还应该使用ItemContainerStyle
将ListView
的{{1}}属性设置为自定义样式:
DataTrigger