一知举、由DataTemplate生成的列表因?yàn)闆]有了Name屬性瞬沦,從而不能直接訪問觸發(fā)事件的控件,所以要通過e.OriginalSource拿到控件對象雇锡,從而更改綁定到控件的數(shù)據(jù)逛钻。
MainWindow.xaml.cs文件代碼(按鍵盤→鍵可以看到右側(cè)被擋住的代碼)
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
{
/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
public partial class MainWindow : Window
{
public Studnets students { get; set; }
public MainWindow()
{
InitializeComponent();
students = new Studnets();
//添加幾條數(shù)據(jù)
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; //設(shè)置窗口數(shù)據(jù)上下文為當(dāng)前對象,ListBox控件會自動查找到students集合屬性
}
//事件處理函數(shù):找到按鈕所在行锰提,修改對應(yīng)的Age屬性
private void ListBox_Click(object sender, RoutedEventArgs e)
{
Button btn = e.OriginalSource as Button;//查找到單擊的按鈕
var cp = btn.TemplatedParent as ContentPresenter;//查找到動態(tài)生成的單行ContentPresenter對象
Student student = cp.Content as Student;//查找到ContentPresenter對象的數(shù)據(jù)
student.Age += 1;//更改數(shù)據(jù)
}
}
//數(shù)據(jù)類
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<Student> { }
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文件代碼(按鍵盤→鍵可以看到右側(cè)被擋住的代碼)
<Window x:Class="WpfApp3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp3"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ResourceDictionary>
<local:BoolToBrushConverter x:Key="BoolToBrushConverter"/>
</ResourceDictionary>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding students}" HorizontalAlignment="Left"
Height="271" Margin="22,26,0,0" VerticalAlignment="Top" Width="332"
ButtonBase.Click="ListBox_Click">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Name}" Width="100"/>
<Rectangle Width="10" Height="10" Margin="5" Fill="{Binding IsGood, Converter={StaticResource BoolToBrushConverter}}"/>
<Label Content="{Binding Age}" Width="50"/>
<Button Content="增加一歲" Width="50"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
代碼效果如下:
代碼效果