someone1 someone1 - 4 years ago 241
C# Question

ListView item visibility based on boolean

I am trying to filter ListView Items based on input text, and because the selection is removed, I am trying to set visibilty to invisible based on a boolean(I hope this wont remove the selection of items)

I used some older posts to create this code, but I can't figure out why it isn't working:

<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>


Text filter method(which is working fine):

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 { }
}


I found the code here

Note that I am not using any data binding. What am I missing?

mm8 mm8
Answer Source

Make sure that the Oseba class implements the INotifyPropertyChanged interface and raises the PropertyChanged event in the setter of the visible property:

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));
    }
}

This is required for the data trigger in your ListViewItem style to get triggered dynamically.

You should also set the ItemContainerStyle property of the ListView to your custom style with the DataTrigger:

<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>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download