2017-02-06 68 views
1

我想基於輸入文本篩選ListView項目,並且因爲選擇被刪除,我試圖將可見性設置爲基於布爾值的不可見(我希望這將不會刪除項目的選擇)基於布爾值的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

請注意,我沒有使用任何數據綁定。我錯過了什麼?

+0

是否Oseba類實現INotifyPropertyChanged接口? – mm8

+0

在他正在使用''的鏈接中,無法在代碼中找到它。 'o.visible'沒有做任何事情,我猜... –

+0

它沒有。我怎樣才能實現它?我認爲設置這樣的屬性就足夠了,因爲我沒有在那篇文章中找到任何額外的代碼 – someone1

回答

1

確保Oseba類實現INotifyPropertyChanged接口,並在visible setter方法提高了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風格獲得動態觸發。

你也應該在ListViewItemContainerStyle屬性設置爲自定義樣式與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> 
+0

它的工作原理,非常感謝!它也不會刪除選項,就像我希望的那樣。爲什麼這個界面需要?據我瞭解該財產觸發事件或類似的事情?對不起,我的可憐的知識,我只有一年到C#:) – someone1

+0

這是需要更改綁定客戶端和數據源之間的綁定發生的通知,如MSDN文檔中所述:https:// msdn .microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v = vs.110).aspx – mm8

+0

Allright將對此進行調查。我想,一旦我達到代表的級別,它就會顯示出來,我想。 :) – someone1