WPF-取消注释行后,为什么我的UserControl停止工作?当它被注释时,我的控件在未注释时在启动时添加行,它不是通过LoadRow()添加行,甚至没有通过某些文本框和按钮功能尝试在运行时添加行吗?
来自自定义UserControl的代码背后:
public void LoadRows()
{
Rows.Add(new Row("221 331,44", GetOutputFromInput));
Rows.Add(new Row("2 331,44", GetOutputFromInput));
Rows.Add(new Row("331,44", GetOutputFromInput));
Rows.Add(new Row("0,44", GetOutputFromInput));
Rows.Add(new Row { Input = "333", Output = "555"});
Rows.Add(new Row { Input = "333", Output = "555"});
}
//public DependencyProperty RowsProperty = DependencyProperty.Register("Rows", typeof(ObservableCollection<Row>), typeof(TriggerListAdd));
public ObservableCollection<Row> Rows
{
get;
set;
}
您仅通过取消注释该行就不会获得依赖项属性。您还必须编写正确的CLR属性包装器:
public DependencyProperty RowsProperty = DependencyProperty.Register(
nameof(Rows),
typeof(ObservableCollection<Row>),
typeof(TriggerListAdd));
public ObservableCollection<Row> Rows
{
get { return (ObservableCollection<Row>)GetValue(RowsProperty );
set { SetValue(RowsProperty, value);
}
我还建议将属性的类型更改为IEnumerable
,以在使用控件时获得更大的灵活性。