【发布时间】:2011-03-23 14:55:15
【问题描述】:
在我的 MainWindow 中,我有一个 ObservableCollection,它显示在每个绑定的列表框中。
如果我更新我的收藏,修改会显示在列表中。
这行得通:
public ObservableCollection<double> arr = new ObservableCollection<double>();
public MainWindow()
{
arr.Add(1.1);
arr.Add(2.2);
testlist.DataContext = arr;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
arr[0] += 1.0;
}
<ListBox Name="testlist" ItemsSource="{Binding}"></ListBox>
此版本无效:
public ObservableCollection<double> arr = new ObservableCollection<double>();
public MainWindow()
{
arr.Add(1.1);
arr.Add(2.2);
testlist.DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
arr[0] += 1.0;
}
<ListBox Name="testlist" ItemsSource="{Binding Path=arr}"></ListBox>
你能告诉我为什么吗? 我想将 this 作为 DataContext,因为我的对话框中还有许多其他属性要显示,如果我不必为每个单独的控件设置 DataContext,那就太好了。
【问题讨论】: