WPF例7-动态控件修改绑定的列表数据

一、由DataTemplate生成的列表因为没有了Name属性,从而不能直接访问触发事件的控件,所以要通过e.OriginalSource拿到控件对象,从而更改绑定到控件的数据。

MainWindow.xaml.cs文件代码(按键盘→键可以看到右侧被挡住的代码)

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace WpfApp3
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public Studnets students { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            students = new Studnets();
            //添加几条数据
            students.Add(new Student { IsGood = false, Name = "Tom", Age=18 });
            students.Add(new Student { IsGood = false, Name = "Peter", Age=20 });
            students.Add(new Student { IsGood = true, Name = "Hank", Age=23 });
            students.Add(new Student { IsGood = false, Name = "Nancy", Age=30 });
            students.Add(new Student { IsGood = true, Name = "Joe", Age=12 });
            DataContext = this; //设置窗口数据上下文为当前对象,ListBox控件会自动查找到students集合属性
        }

        //事件处理函数:找到按钮所在行,修改对应的Age属性
        private void ListBox_Click(object sender, RoutedEventArgs e)
        {
            Button btn = e.OriginalSource as Button;//查找到单击的按钮
            var cp = btn.TemplatedParent as ContentPresenter;//查找到动态生成的单行ContentPresenter对象
            Student student = cp.Content as Student;//查找到ContentPresenter对象的数据
            student.Age += 1;//更改数据
        }
    }
    //数据类
    public class Student:DependencyObject
    {
        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Name.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NameProperty =
            DependencyProperty.Register("Name", typeof(string), typeof(Student));


        public bool IsGood
        {
            get { return (bool)GetValue(IsGoodProperty); }
            set { SetValue(IsGoodProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsGood.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsGoodProperty =
            DependencyProperty.Register("IsGood", typeof(bool), typeof(Student));


        public int Age
        {
            get { return (int)GetValue(AgeProperty); }
            set { SetValue(AgeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Age.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AgeProperty =
            DependencyProperty.Register("Age", typeof(int), typeof(Student));


    }
    public class Studnets : ObservableCollection { }
    public class BoolToBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? Brushes.Green : Brushes.Red;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

MainWindow.xaml文件代码(按键盘→键可以看到右侧被挡住的代码)


    
        
            
        
    
    
        
            
                
                    
                        
                            

代码效果如下:


WPF例7-动态控件修改绑定的列表数据_第1张图片
代码效果

你可能感兴趣的:(WPF例7-动态控件修改绑定的列表数据)