元素编号更改时绑定到List元素


private List<Report> _reports = new List<Report>();
public Report CurrentReport
{
get { return _reports[_componentIterator]; }
set { _reports[_componentIterator] = value; }
}

我有一个_reports字段,它是一个Report对象列表。

我使用CurrentReport属性来访问基于_componentIterator的当前Report对象。

如何绑定到某些Report属性,以便更改_componentIterator不会破坏我的绑定?

如果我这样绑定,那么每次_componentIterator的更改都会破坏绑定。

Binding designatorTextBlockBinding = new Binding(nameof(CurrentReport.Designator));
designatorTextBlockBinding.Source = CurrentReport;
_artifactControl.DesignatorTextBlock.SetBinding(Controls.TextBlock.TextProperty, designatorTextBlockBinding);

如下所示声明绑定,并确保CurrentReport属性触发更改通知。

var designatorTextBlockBinding = new Binding
{
Path = new PropertyPath("CurrentReport.Designator"),
Source = this
};

var designatorTextBlockBinding = new Binding
{
Path = new PropertyPath(
nameof(CurrentReport) + "." + nameof(CurrentReport.Designator)),
Source = this
};

var designatorTextBlockBinding = new Binding(
nameof(CurrentReport) + "." + nameof(CurrentReport.Designator))
{
Source = this
};

最新更新