DataGrid фильтрация, мульти-вход, MVVM, C# - PullRequest
1 голос
/ 24 февраля 2020

Итак, у меня возникли некоторые затруднения, когда я понял это: у меня есть DataGrid, который я пытаюсь отфильтровать. Я использую ICollectionView в качестве ItemsSource. У меня есть пара ComboBox, для которых я хотел бы сузить данные, затем у меня также есть TextBox, я бы хотел, чтобы пользователи сузили данные дальше.

Моя проблема в том, что я могу заставить TextBox фильтровать данные, и я могу заставить ComboBox фильтровать данные, но я не могу заставить их работать вместе для дальнейшей фильтрации данных. Только один фильтр работает одновременно.

public static string FilterText
{
   get { return _filterText; }
   set
   {
      _filterText = value;
      ICollectionView.Filter = FilterTB;
   }
}

public static Model1 PublicModelProperty
{
   get { return _publicModelProperty; }
   set
   {
      _publicModelProperty = value;
      ICollectionView.Filter = FilterCB;
   }
}

public static bool FilterTB(object names)
{
   Model2 name = names as Model2;
   if(!string.IsNullOrEmpty(FilterText))
   {
      return name.Property1.Contains(FilterText) ||
             name.Property2.Contains(FilterText);
   }
   else
   {
      return true;
   }
}

public static bool FilterCB(object names)
{
   Model2 name = names as Model2;
   if(!string.IsNullOrEmpty(PublicModelProperty.Property))
   {
      return name.Property3.Contains(PublicModelProperty.Property);
   }
   else
   {
      return true;
   }
}

1 Ответ

1 голос
/ 24 февраля 2020

Вы должны фильтровать, используя FilterText и PublicModelProperty

public static string FilterText
{
   get { return _filterText; }
   set
   {
      _filterText = value;
      ICollectionView.Filter = FilterBoth;
   }
}

public static Model1 PublicModelProperty
{
   get { return _publicModelProperty; }
   set
   {
      _publicModelProperty = value;
      ICollectionView.Filter = FilterBoth;
   }
}

public static bool FilterBoth(object names)
{
    Model2 name = names as Model2;
    if (!string.IsNullOrEmpty(FilterText))
    {
        if (!name.Property1.Contains(FilterText) &&
                !name.Property2.Contains(FilterText))
            return false;
    }
    if (!string.IsNullOrEmpty(PublicModelProperty.Property))
    {
        if (!name.Property3.Contains(PublicModelProperty.Property))
            return false;
    }
    return true;
}
...