2009/04/03(Fri) 01:41:20 編集(投稿者)
noriさん、ありがとうございます_(__)_
下記のコードで動作確認しました!
(A)でINotifyPropertyChangedつかうコードも挑戦してみます。
using System.Collections.ObjectModel;
namespace WPFTest3
{
/// <summary>
/// Window1.xaml の相互作用ロジック
/// </summary>
public partial class Window1 : Window
{
public class Data : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string name;
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
OnPropertyChanged("Name");
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged == null) return;
var args = new PropertyChangedEventArgs(propertyName);
this.PropertyChanged(this, args);
}
}
ObservableCollection<Data> list;
public Window1()
{
InitializeComponent();
list = new ObservableCollection<Data>();
list.Add(new Data { Name = "name1" });
this.listView1.DataContext = list;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
list.Add(new Data { Name = "name2" });
}
private void button2_Click(object sender, RoutedEventArgs e)
{
list[0].Name = "変更";
}
}
}
<Window x:Class="WPFTest3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<ListView Margin="68,66,90,96" ItemsSource="{Binding}" Name="listView1">
<ListView.View>
<GridView>
<GridViewColumn Header="name" DisplayMemberBinding="{Binding Name}"/>
</GridView>
</ListView.View>
</ListView>
<Button Height="23" Margin="0,26,50,0" Name="button1" VerticalAlignment="Top" Click="button1_Click" HorizontalAlignment="Right" Width="74">Button</Button>
<Button Height="23" HorizontalAlignment="Left" Margin="40,26,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click">Button</Button>
</Grid>
</Window>