【问题标题】:Implement Visibility Property to a custom class将可见性属性实现到自定义类
【发布时间】:2015-05-11 22:15:47
【问题描述】:

我创建了一个类,我需要像其他 UI 控件一样具有 Visibility 属性。它看起来像这样:

更多扩展代码:

xaml:

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

后面的代码:

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

我尝试做的是过滤器(搜索),我想隐藏项目。仅仅向类添加一个属性也是行不通的。

【问题讨论】:

  • 定义“不起作用”
  • 只需添加一个可见性属性
  • @WillemvanRumpt 它不会折叠
  • 那么我们需要更多的代码和/或xaml来分析问题。可见性在 UIElement 中定义,并且“正常工作”。
  • 您没有在任何地方显示您的控件。对于 ListBox 的 ItemsSource 中的每个“MyClass”,您将显示一个带有两个 TextBlock 的 Grid。在 DataTemplate 中使用 MyClass。您需要阅读 DataTemplates (msdn.microsoft.com/en-us/library/windows/apps/…)

标签: c# windows-runtime windows-store-apps windows-store


【解决方案1】:

你非常接近......要让这个工作你的类 MyClass 必须实现 INotifyPropertyChanged,我使用基类可绑定,这使得实现 INotifyPropertyChanged 变得更加简单。

下面是答案

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>

代码:

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

【讨论】:

    猜你喜欢
    • 2013-04-10
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    相关资源
    最近更新 更多