所以,我创建了一个示例来显示我目前在WPF应用程序中遇到的问题。首先,我有一个餐馆对象,包含名称,以及我打算绑定到CheckBox.Checked值的选定变量。我将它附加到ComboBox和ListBox作为其ItemsSource。
public class Restaurants
{
public String Name { get; set; }
public Boolean Selected { get; set; }
}
rItems = new List<Restaurants>();
rItems.Add(new Restaurants() { Name = "Alice Fazoolis", Selected = false });
rItems.Add(new Restaurants() { Name = "Applebees", Selected = false });
rItems.Add(new Restaurants() { Name = "Caseys", Selected = false });
rItems.Add(new Restaurants() { Name = "Cracker Barrell", Selected = false });
rItems.Add(new Restaurants() { Name = "East Side Marios", Selected = false });
rItems.Add(new Restaurants() { Name = "Golden Griddle", Selected = false });
rItems.Add(new Restaurants() { Name = "Harveys", Selected = true });
rItems.Add(new Restaurants() { Name = "Imperial Buffet", Selected = false });
rItems.Add(new Restaurants() { Name = "Jack Astors", Selected = false });
rItems.Add(new Restaurants() { Name = "Kakimono", Selected = false });
rItems.Add(new Restaurants() { Name = "Kelseys", Selected = false });
rItems.Add(new Restaurants() { Name = "Little Ceasars", Selected = false });
rItems.Add(new Restaurants() { Name = "Lonestar", Selected = false });
rItems.Add(new Restaurants() { Name = "Makimono", Selected = false });
rItems.Add(new Restaurants() { Name = "Mandarin", Selected = false });
rItems.Add(new Restaurants() { Name = "McDonalds", Selected = false });
rItems.Add(new Restaurants() { Name = "Milestones", Selected = false });
rItems.Add(new Restaurants() { Name = "Montanas", Selected = false });
rItems.Add(new Restaurants() { Name = "Moxies", Selected = false });
rItems.Add(new Restaurants() { Name = "Mr. Sub", Selected = false });
rItems.Add(new Restaurants() { Name = "None", Selected = false });
rItems.Add(new Restaurants() { Name = "Other (Specify)", Selected = false });
rItems.Add(new Restaurants() { Name = "Pizza Pizza", Selected = false });
rItems.Add(new Restaurants() { Name = "Spring Rolls", Selected = false });
rItems.Add(new Restaurants() { Name = "Subway", Selected = false });
rItems.Add(new Restaurants() { Name = "St. Louis Bar & Grill", Selected = false });
rItems.Add(new Restaurants() { Name = "Sunset Grill", Selected = false });
rItems.Add(new Restaurants() { Name = "Swiss Chalet", Selected = false });
rItems.Add(new Restaurants() { Name = "Tatemono", Selected = false });
rItems.Add(new Restaurants() { Name = "The Keg", Selected = false });
rItems.Add(new Restaurants() { Name = "The Melting Pot", Selected = false });
rItems.Add(new Restaurants() { Name = "The Outback", Selected = false });
rItems.Add(new Restaurants() { Name = "Tim Hortons", Selected = false });
rItems.Add(new Restaurants() { Name = "Wendys", Selected = false });
rItems.Add(new Restaurants() { Name = "Wimpys Diner", Selected = false });
rItems.Add(new Restaurants() { Name = "Quiznos", Selected = false });
cboSelections.ItemsSource = rItems;
lstAnswers.ItemsSource = rItems;
然后我有两种过滤方法。一个用于选择以特定字母开头的所有值,第二个用于获取所有选定的项目。在我的屏幕上,我生成了每个起始字母的键盘。选择一个字母后,我会过滤列表并仅显示这些结果。选定的组将被放入ListBox
private bool LetterFilter(object item){
if (((Restaurants) item).Name.ToUpper().StartsWith(LetterClicked))
return true;
else
return false;
}
private bool SelectedFilter(object item){
if (((Restaurants)item).Selected)
return true;
else
return false;
}
当Window_Loaded时,我根据一封我已知不会对ListBox产生任何结果的信件进行过滤。并告诉我的选择框以获取任何选定的值。 (我希望启动时屏幕为空)
private void Window_Loaded(object sender, RoutedEventArgs e)
{
CollectionView viewCBO = (CollectionView)CollectionViewSource.GetDefaultView(cboSelections.ItemsSource);
viewCBO.Filter = SelectedFilter;
CollectionView viewLST = (CollectionView)CollectionViewSource.GetDefaultView(lstAnswers.ItemsSource);
LetterClicked = "-";
viewLST.Filter = LetterFilter;
}`
private void btnLetter_Click(object sender, RoutedEventArgs e)
{
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lstAnswers.ItemsSource);
LetterClicked = ((Button)sender).Content.ToString();
view.Filter = LetterFilter;
}
问题是,当我加载任何一个过滤器时,我最终在viewCBO和viewLST中获得相同的值。因此,例如,如果我选择“L”,我会看到两个中的“小凯撒”和“孤星”,尽管没有选择任何一个。
我需要的是viewLST使我的答案列表的值按字母过滤,而viewCBO则为所有Selected = true的单独列表。但是来自同一个来源。
感谢所有帮助:)
答案 0 :(得分:1)
如果对CollectionViewSource使用具有相同列表的静态方法,它将具有相同的源。请参阅以下代码。
<Window x:Class="MSDN15Jan2015_Learning.Window4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window4" Height="600" Width="300">
<StackPanel>
<TextBox x:Name="txt" Height="20" Width="200"/>
<Button x:Name="btn" Click="btn_Click" Content="Letter Refresh"/>
<ListBox x:Name="lstAnswers" Height="200" SelectionMode="Multiple" SelectionChanged="lstAnswers_SelectionChanged">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding Selected}"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<CheckBox IsChecked="{Binding Selected}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ComboBox DisplayMemberPath="Name" x:Name="cboSelections" Height="20"/>
</StackPanel>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MSDN15Jan2015_Learning
{
/// <summary>
/// Interaction logic for Window4.xaml
/// </summary>
public partial class Window4 : Window
{
ObservableCollection<Restaurants> rItems;
string LetterClicked;
public Window4()
{
InitializeComponent();
this.Loaded += Window4_Loaded;
rItems = new ObservableCollection<Restaurants>();
rItems.Add(new Restaurants() { Name = "Alice Fazoolis", Selected = false });
rItems.Add(new Restaurants() { Name = "Applebees", Selected = false });
rItems.Add(new Restaurants() { Name = "Caseys", Selected = false });
rItems.Add(new Restaurants() { Name = "Cracker Barrell", Selected = false });
rItems.Add(new Restaurants() { Name = "East Side Marios", Selected = false });
rItems.Add(new Restaurants() { Name = "Golden Griddle", Selected = false });
rItems.Add(new Restaurants() { Name = "Harveys", Selected = false });
rItems.Add(new Restaurants() { Name = "Imperial Buffet", Selected = false });
rItems.Add(new Restaurants() { Name = "Jack Astors", Selected = false });
rItems.Add(new Restaurants() { Name = "Kakimono", Selected = false });
rItems.Add(new Restaurants() { Name = "Kelseys", Selected = false });
rItems.Add(new Restaurants() { Name = "Little Ceasars", Selected = false });
rItems.Add(new Restaurants() { Name = "Lonestar", Selected = false });
rItems.Add(new Restaurants() { Name = "Makimono", Selected = false });
rItems.Add(new Restaurants() { Name = "Mandarin", Selected = false });
rItems.Add(new Restaurants() { Name = "McDonalds", Selected = false });
rItems.Add(new Restaurants() { Name = "Milestones", Selected = false });
rItems.Add(new Restaurants() { Name = "Montanas", Selected = false });
rItems.Add(new Restaurants() { Name = "Moxies", Selected = false });
rItems.Add(new Restaurants() { Name = "Mr. Sub", Selected = false });
rItems.Add(new Restaurants() { Name = "None", Selected = false });
rItems.Add(new Restaurants() { Name = "Other (Specify)", Selected = false });
rItems.Add(new Restaurants() { Name = "Pizza Pizza", Selected = false });
rItems.Add(new Restaurants() { Name = "Spring Rolls", Selected = false });
rItems.Add(new Restaurants() { Name = "Subway", Selected = false });
rItems.Add(new Restaurants() { Name = "St. Louis Bar & Grill", Selected = false });
rItems.Add(new Restaurants() { Name = "Sunset Grill", Selected = false });
rItems.Add(new Restaurants() { Name = "Swiss Chalet", Selected = false });
rItems.Add(new Restaurants() { Name = "Tatemono", Selected = false });
rItems.Add(new Restaurants() { Name = "The Keg", Selected = false });
rItems.Add(new Restaurants() { Name = "The Melting Pot", Selected = false });
rItems.Add(new Restaurants() { Name = "The Outback", Selected = false });
rItems.Add(new Restaurants() { Name = "Tim Hortons", Selected = false });
rItems.Add(new Restaurants() { Name = "Wendys", Selected = false });
rItems.Add(new Restaurants() { Name = "Wimpys Diner", Selected = false });
rItems.Add(new Restaurants() { Name = "Quiznos", Selected = false });
}
ICollectionView viewCBO;
ICollectionView viewLST;
void Window4_Loaded(object sender, RoutedEventArgs e)
{
viewCBO = new CollectionViewSource { Source = rItems }.View;
viewCBO.Filter = SelectedFilter;
viewLST = new CollectionViewSource { Source = rItems }.View;
LetterClicked = "-";
viewLST.Filter = LetterFilter;
cboSelections.ItemsSource = viewCBO;
lstAnswers.ItemsSource = viewLST;
}
private bool LetterFilter(object item)
{
if (((Restaurants)item).Name.ToUpper().StartsWith(LetterClicked))
return true;
else
return false;
}
private bool SelectedFilter(object item)
{
if (((Restaurants)item).Selected)
return true;
else
return false;
}
private void btn_Click(object sender, RoutedEventArgs e)
{
LetterClicked =txt.Text;
viewLST.Filter = null;
viewLST.Filter = LetterFilter;
viewLST.Refresh();
}
private void lstAnswers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
viewCBO.Refresh();
}
}
public class Restaurants:INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set { name = value; OnNotify("Name"); }
}
private bool selected;
public bool Selected
{
get { return selected; }
set { selected = value; OnNotify("Selected"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnNotify(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
}