在多种条件下过滤不起作用

我正在编写一个将虚拟数据加载到数据网格的 WPF 应用程序。数据网格包含以下数据模型:


public class CharacterCollection

  {

    public string Name { get; set; }

    public int Level { get; set; }

    public string Race { get; set; }

    public string Class { get; set; }


    public List<CharacterCollection> GetCharacters()

    {

      List<CharacterCollection> characters = new List<CharacterCollection>();

      characters.Add(new CharacterCollection { Name = "Lothar", Class = "Fighter", Race = "Human", Level = 5 });

      characters.Add(new CharacterCollection { Name = "Turk", Class = "Barbarian", Race = "Half-Orc", Level = 3 });

      characters.Add(new CharacterCollection { Name = "Melian", Class = "Cleric", Race = "Elf", Level = 10 });

      //... there's about 16 more entries, but you get the idea ;)

      return characters;

    }

接下来,我添加了 4 个组合框;每个都填充了 CharacterCollection 中的一个属性


public partial class MainWindow : Window

{

    private CharacterCollection _characters = new CharacterCollection();

    List<CharacterCollection> collection = new List<CharacterCollection>();


    public MainWindow()

    {

      InitializeComponent();


      collection = _characters.GetCharacters();

      dgCharacterChart.ItemsSource = collection;


      var characterNames = collection.Select(c =>c.Name).Distinct().ToList();

      foreach(var item in characterNames)

      {

        CheckBox cb = new CheckBox();

        cb.Content = item.ToString();


        cbName.Items.Add(cb);

      }

      //...the other 3 combo boxes are filled the same way. I know its ugly and I will work on abstracting this a little better :)

   }

}

到目前为止,我得到的是 4 个组合框,其中填充了复选框,用户可以根据需要进行检查。这些将用于过滤。


当我单击仅选择一个字段的按钮时,例如从“名称”组合框中,它过滤得很好。但是,如果我从Name 中选择了多个,或者如果我开始检查其他组合框中的多个条目,则我的数据网格显示为空白。当我调试我的代码时,我可以看到我的谓词列表包含我所有标记为已检查的条目,但我的集合计数为 0。有人可以帮我弄清楚为什么我会得到这些结果吗?


BIG阳
浏览 197回答 1
1回答

MYYA

我做了类似的事情,但我通过文本框过滤。也许它会帮助你。.Xaml 如下&nbsp;<TextBox FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Background="{x:Null}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Text="{Binding Item , UpdateSourceTrigger=LostFocus}" Margin="6,0,0,0" BorderThickness="0" PreviewKeyDown="ItemField_PreviewKeyDown" TextChanged="ItemField_TextChanged" IsReadOnly="{Binding IsReadonly}" />.Xaml.cs 如下private ObservableCollection<ItemGrid> _itemGrid = new ObservableCollection<ItemGrid>();&nbsp; &nbsp; public ObservableCollection<ItemGrid> ItemGrid&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _itemGrid;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _itemGrid = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private void ItemField_TextChanged(object sender, TextChangedEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (isBeginingEdit) return;&nbsp; &nbsp; &nbsp; &nbsp; //here we show the item selector and take care of autocomplete&nbsp; &nbsp; &nbsp; &nbsp; var textBox = sender as TextBox;&nbsp; &nbsp; &nbsp; &nbsp; if (textBox.Text != "")&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var _itemSourceList = new CollectionViewSource() { Source = ItemGrid };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ICollectionView Itemlist = _itemSourceList.View;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ItemSearchText = textBox.Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Itemlist.Filter = ItemFilter;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var count = _itemSourceList.View.Cast<ItemGrid>().Count();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (count > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ItemsGrid.ItemsSource = Itemlist;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP