来自同一来源的 2 个不同过滤的绑定,结果相同(WPF)
2 differently filtered bindings from same source, coming out with same results (WPF)
因此,我创建了一个示例来说明我的 WPF 应用程序目前遇到的问题。首先,我有一个餐厅对象,其中包含名称和一个 selected 变量,我打算将其绑定到 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;
然后我有两种过滤方法。一个是 select 以特定字母开头的所有值,第二个是获取所有 selected 的项目。在我的屏幕上,我生成了每个起始字母的键盘。当一个字母被 selected 时,我过滤列表并只显示那些结果。 selected 组将被放入 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 时,我根据一封我知道不会有列表框结果的信件进行过滤。并告诉我的 selections 框获取任何 selected 值。 (我希望启动时屏幕为空)
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 中得到相同的值。因此,例如,如果我 select "L",我会在两者中看到 "Little Caesars" 和 "Lonestar",尽管没有 select 任何一个。
我需要的是让 viewLST 按字母过滤我的答案列表的值,而对于 viewCBO,我需要一个单独的列表,用于所有已选择 = true 的答案。但来自同一来源。
感谢所有帮助:)
如果您对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));
}
}
}
}
因此,我创建了一个示例来说明我的 WPF 应用程序目前遇到的问题。首先,我有一个餐厅对象,其中包含名称和一个 selected 变量,我打算将其绑定到 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;
然后我有两种过滤方法。一个是 select 以特定字母开头的所有值,第二个是获取所有 selected 的项目。在我的屏幕上,我生成了每个起始字母的键盘。当一个字母被 selected 时,我过滤列表并只显示那些结果。 selected 组将被放入 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 时,我根据一封我知道不会有列表框结果的信件进行过滤。并告诉我的 selections 框获取任何 selected 值。 (我希望启动时屏幕为空)
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 中得到相同的值。因此,例如,如果我 select "L",我会在两者中看到 "Little Caesars" 和 "Lonestar",尽管没有 select 任何一个。
我需要的是让 viewLST 按字母过滤我的答案列表的值,而对于 viewCBO,我需要一个单独的列表,用于所有已选择 = true 的答案。但来自同一来源。
感谢所有帮助:)
如果您对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));
}
}
}
}