これは人のコレクションの最初の人物の年齢をテキストボックスに表示する簡単なxamlです。私は年齢が変化していないクリック後に私は理解していない。Wpf - コレクションアイテムへのバインドに関する問題
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="132*" />
<RowDefinition Height="179*" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Persons[0].Age}" />
<Button Grid.Row="1" Click="Button_Click">Change Age</Button>
</Grid>
これは、XAMLの背後にあるコードです:
public partial class MainWindow : Window
{
public ObservableCollection<Person> Persons { get; set; }
public MainWindow() {
Persons = new ObservableCollection<Person>();
Persons.Add(new Person{Age = -1});
DataContext = this;
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e) {
(Persons[0] as Person).Age = 5;
}
}
これは、クラスの人です:ビューは一つの要素の1つの特性をキャッチしていないので、おそらくです
public class Person : INotifyPropertyChanged
{
private int _age;
public int Age
{
get { return _age; }
set
{
_age = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Age"));
}
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
Visual Studio 2010のWPFプロジェクトにちょうど貼り付けたときにコードが正常に機能しました。 WPFとVisual Studioのどのバージョンを使用していますか? –
私のためにうまく働いた。プロジェクトにコピー/ペーストすると、チャームのように機能します。 Ageにsetでブレークポイントを設定すると、PropertyChangedが発生するか、それともnullですか? –