我在 XAML 中绑定静态属性时遇到问题。基于这个答案。我在我的类中定义了一个静态变量:
class Worker
{
public static ObservableCollection<int> CountInDirections { get; set; }
static Worker()
{
CountInDirections = new ObservableCollection<int>
{
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
}
}
但 XAML 代码中出现以下错误:
Член "CountInDirections[6]" не распознан или недоступен.
这是 XAML 中的内容:
<Label Content="{Binding Path=(local:Worker.CountInDirections[6])}"/>
访问索引时出现错误。
怎么修?
要在绑定时按索引访问静态集合,请使用以下方法:
试试这样: