我想在我的UserControl中有一個圖形部分,它將根據集合顯示行(並在運行時更新座標,並在運行時基於集合添加/刪除行)。將ObservableCollection綁定到行
讓我們假設我有一個名爲Class 1類:
public class Class1 : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, e);
}
private int _x1 = 0;
public int X1
{
get { return _x1; }
set
{
_x1 = value;
OnPropertyChanged(new PropertyChangedEventArgs("X1"));
}
}
}
在我的用戶而一個ObservableCollection:
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, e);
}
private ObservableCollection<Class1> _classes = new ObservableCollection<Class1>();
public ObservableCollection<Class1> Classes
{
get { return _classes; }
}
如果我知道線的量,我可以在創建的行數我的用戶控件是這樣的:
XAML:
<Grid DataContext="{Binding ElementName=LayoutRoot}">
<Line X1="{Binding Items[0].X1}" X2="100" Y1="50" Y2="100" Stroke="Red" StrokeThickness="4"/>
<Line X1="{Binding Items[1].X1}" ... />
...
</Grid>
我該如何繼續?
感謝您的努力