포인트 목록이 포함 된 ViewModel 클래스가 있는데이를 Polyline에 바인딩하려고합니다. Polyline은 초기 포인트 목록을 선택하지만 INotifyPropertyChanged를 구현하더라도 추가 점이 추가되면이를 알지 못합니다. 뭐가 문제 야?이 데이터 바인딩이 작동하지 않는 이유는 무엇입니까?
<StackPanel>
<Button Click="Button_Click">Add!</Button>
<Polyline x:Name="_line" Points="{Binding Pts}" Stroke="Black" StrokeThickness="5"/>
</StackPanel>
C#을 측면 : 컬렉션에 바인딩되어 있기 때문에, 그것은 ObservableCollection<T>
뭔가를해야한다는 매우 가능성이
// code-behind
_line.DataContext = new ViewModel();
private void Button_Click(object sender, RoutedEventArgs e)
{
// The problem is here: NOTHING HAPPENS ON-SCREEN!
((ViewModel)_line.DataContext).AddPoint();
}
// ViewModel class
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public PointCollection Pts { get; set; }
public ViewModel()
{
Pts = new PointCollection();
Pts.Add(new Point(1, 1));
Pts.Add(new Point(11, 11));
}
public void AddPoint()
{
Pts.Add(new Point(25, 13));
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Pts"));
}
}
답변이 업데이트되어 원인을 찾았습니다. – Carlo