【问题标题】:WPF: two controls binding to one source, how to filter the binding?WPF:两个控件绑定到一个源,如何过滤绑定?
【发布时间】:2012-09-14 04:51:42
【问题描述】:

我有一个 WPF windows 应用程序项目,它有一个带有两个 ListBox 控件的窗口,问题是如何将一个源绑定到这两个控件? 来源是这样的:

class student
{
    public string name{get;set;}
    public int age{get;set;}
}

ObservableCollection<student> m_myGroup;

我想:如果年龄 > 25,ListBox1 绑定到 m_myGroup 如果年龄

TextBlock Text={binding Path=name}

并且我不想使用 DataTrigger 将项目可见性属性隐藏或显示,我尝试使用 ICollectionView 过滤源,但它会影响其他 ListBox! 有谁知道如何为每个 ListBox 制作两个过滤器,并且它们只绑定到一个源?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    为 m_myGroup 创建两个ICollectionView,过滤它们并将它们绑定到ListBoxes

    就此而言,将ListBox.IsSynchronizedWithCurrentItem 设置为false 将不会在选择时ListBoxes 之间产生影响。

    this

    编辑:

    public class StudentHandler
    {
        ObservableCollection<student> m_myGroup;
    
        public CollectionViewSource YoungStudentsViewSource { get; private set; }
        public CollectionViewSource OldStudentsViewSource { get; private set; }
    
        public StudentHandler
        {
            YoungStudentsViewSource = new CollectionViewSource {Source = m_myGroup};
            OldStudentsViewSource = new CollectionViewSource {Source = m_myGroup}; 
    
            YoungStudentsViewSource.Filter = (stud) => {return (stud as student).age<=25;};
            OldStudentsViewSource .Filter = (stud) => {return (stud as student).age>25;};
        }
    }
    

    在此之后将ViewSources 绑定到ListBoxes

    【讨论】:

    【解决方案2】:
    <Window x:Class="ComboboxStyle.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:converter="clr-namespace:ComboboxStyle"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <converter:AgeConverter x:Key="ageConv"/>
    </Window.Resources>
    
    <Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <ListBox Grid.Column="0"  DisplayMemberPath="Name" ItemsSource="{Binding Students, Converter={StaticResource ageConv}, ConverterParameter=agelessthan25}" >
        </ListBox>
        <ListBox  Grid.Column="1" DisplayMemberPath="Name" ItemsSource="{Binding Students, Converter={StaticResource ageConv}, ConverterParameter=agegreaterthan25}" >
        </ListBox>
    </Grid>
    

     public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Students = new ObservableCollection<Student>();
            Students.Add(new Student() { Name = "Aeqwwe", Age = 24 });
            Students.Add(new Student() { Name = "bqwewqeq", Age = 28 });
            Students.Add(new Student() { Name = "cwqeqw", Age = 23 });
            Students.Add(new Student() { Name = "dweqqw", Age = 29 });
            Students.Add(new Student() { Name = "eqweweq", Age = 20 });
            DataContext = this;
        }
        public ObservableCollection<Student> Students { get; set; }
    }
    public class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    public class AgeConverter : IValueConverter
    {
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var items = value as ObservableCollection<Student>;
            if (parameter != null && items != null)
            {
                if (parameter.ToString() == "agelessthan25")
                {
                    return items.Where(i => i.Age < 25).ToList();
                }
                else if (parameter.ToString() == "agegreaterthan25")
                {
                    return items.Where(i => i.Age >= 25).ToList();
                }
            }
            return null;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    我希望这会有所帮助

    【讨论】:

    • 非常感谢,作为改进,我也想知道: public class Student { public string Name { get; set; } public int Age { get ; set; } ObservableCollection m_myTeam; } 如何过滤这个?
    • public class Student { public string Name { get; set; } public int Age { get; set; } ObservableCollection m_myTeam; } 如何过滤这个?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多