0

I've created a class that I need to have Visibility property like other UI controls. It looks like this:

More extended code:

xaml:

<ListBox x:Name="itemsHolder" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <TextBlock  Text="{Binding Name}" />
                            <TextBlock  Text="{Binding Surname}"/>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

Code behind:

public ObservableCollection<MyClass > myVM= new ObservableCollection<MyClass>();

        public class MyClass : Control //FrameworkElement
                {
                    public string Name { get; set; }


                    public string Surname { get; set; }
                }
    ...
    MyClass my1 = new MyClass();
    my1.Name = "Test";
    my1.Surname = "Test2";
    myVM.Add(my1);
    itemsHolder.ItemsSource = myVm;
    ...

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
      {
        foreach(MyClass item in itemsHolder.Items)
        {
           if(!item.Name.Contains((sender as TextBox).Text))
           {
               item.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
           }
           else
           {
               item.Visibility = Windows.UI.Xaml.Visibility.Visible;
           }
        }
      }

What I try to do is something like a filter(search) and I want to hide items. Just adding a property to the class also does not work.

Zara Gheorghe
  • 488
  • 4
  • 13
  • 1
    Define "does not work" – Willem van Rumpt Mar 10 '15 at 13:02
  • 1
    Just add a visibility property – MRebai Mar 10 '15 at 13:10
  • @WillemvanRumpt it does not become Collapsed – Zara Gheorghe Mar 10 '15 at 13:18
  • Then more code and/or xaml is required for us to analyze the problem. Visibility is defined in UIElement, and "just works". – Willem van Rumpt Mar 10 '15 at 13:20
  • You're not displaying your control anywhere. For each "MyClass" in the ItemsSource of the ListBox, you're displaying a Grid with two TextBlocks. Use MyClass in the DataTemplate. You'll want to read up on DataTemplates (https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.datatemplate.aspx) – Willem van Rumpt Mar 10 '15 at 13:33
  • How about these solutions: http://www.codeproject.com/Articles/527686/A-WinRT-CollectionView-class-with-Filtering-and-So or http://stackoverflow.com/questions/14497506/collectionviewsource-how-to-filter-data (last link have to be adapted for winrt) – dschüsä Mar 11 '15 at 09:50

1 Answers1

0

You are very close... to get this working your class MyClass must implement INotifyPropertyChanged, I use the base class bindable which makes implementing INotifyPropertyChanged much simpler.

Below is the answer

xaml:

       <ListBox Grid.Row="1" x:Name="itemsHolder" >
              <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Visibility="{Binding IsVisible}">
                        <TextBlock  Text="{Binding Name}" />
                        <TextBlock  Text="{Binding Surname}"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

code:

 public ObservableCollection<MyClass > myVM= new ObservableCollection<MyClass>();



   public class Bindable:INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

 public class MyClass : Bindable   {

      private string _Name;
      public string Name {
          get { return _Name; }
          set
          {
              if (_Name != value)
              {
                  _Name = value;                      
                  OnPropertyChanged();
              }
          } 
      }
      private string _Surname;

      public string Surname
      {
          get { return _Surname; } 
          set{
              if (_Surname != value)
              {
                  _Surname = value;
                  OnPropertyChanged();
              }
          }
      }

      public Visibility _IsVisible;
      public Visibility IsVisible {
          get { return _IsVisible; }
          set {

              if (_IsVisible != value)
              {
                  _IsVisible = value;
                  OnPropertyChanged();
              }
          } 
      }
    }  


private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  {
    foreach(MyClass item in itemsHolder.Items)
    {
       if(!item.Name.Contains((sender as TextBox).Text))
       {
           item.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
       }
       else
       {
           item.Visibility = Windows.UI.Xaml.Visibility.Visible;
       }
    }
  }
Stuart Smith
  • 1,931
  • 15
  • 26