WPF ItemsControl 的 ItemsSource 绑定的一个bug
2009-05-08 19:43
363 查看
不知道是Bug还是什么, 碰到这个问题的.
我测试了一下,所有ItemsControls子类都有这个问题. 譬如: ComboBox
public class NameValue
{
public object Value { get; set; }
public string Name { get; set; }
}
public class TestObj: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void Notify(string propName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
int _v1;
public int V1{
get{ return _v1;}
set{
if (this._v1== value) { return; }
this._v1= value;
Notify("V1");
}
}
int _v2;
public int V2{
get{ return _v2;}
set{
if (this._v2== value) { return; }
this._v2= value;
Notify("V2");
}
}
}
初始化List
List<NameValue> lst = new List<NameValue>();
lst.Add(new NameValue(){Name=”item1”, Value=”1”});
lst.Add(new NameValue(){Name=”item2”, Value=”2”});
lst.Add(new NameValue(){Name=”item3”, Value=”3”});
绑定到ComboBox
cbo1.ItemsSource = lst;
cbo2.ItemsSource = lst;
dataGrid.DataContext = new TestObj(){
V1 = 1,
V2 = 2
};
xaml代码
<Grid x:Name="dataGrid">
<ComboBox x:Name="cbo1" DisplayMemberPath="Name" SelectedValuePath="Value" Width="80" IsSynchronizedWithCurrentItem="True" SelectedValue="{Binding Path=V1}"/>
<ComboBox x:Name="cbo2" DisplayMemberPath="Name" SelectedValuePath="Value" Width="80" IsSynchronizedWithCurrentItem="True" SelectedValue="{Binding Path=V2}"/>
</Grid>
测试看看, 结果竟然是cbo1和cbo2联动了
解决办法
cbo1.ItemsSource = lst.ToArray();
cbo2.ItemsSource = lst.ToArray();
dataGrid.DataContext = new TestObj(){
V1 = 1,
V2 = 2
};
ItemsSource 指向了同一个List导致select同步了, 通过ToArray()方法,其实clone了2个NameValue数组实例, 这样就不会产生select同步的.
我测试了一下,所有ItemsControls子类都有这个问题. 譬如: ComboBox
public class NameValue
{
public object Value { get; set; }
public string Name { get; set; }
}
public class TestObj: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void Notify(string propName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
int _v1;
public int V1{
get{ return _v1;}
set{
if (this._v1== value) { return; }
this._v1= value;
Notify("V1");
}
}
int _v2;
public int V2{
get{ return _v2;}
set{
if (this._v2== value) { return; }
this._v2= value;
Notify("V2");
}
}
}
初始化List
List<NameValue> lst = new List<NameValue>();
lst.Add(new NameValue(){Name=”item1”, Value=”1”});
lst.Add(new NameValue(){Name=”item2”, Value=”2”});
lst.Add(new NameValue(){Name=”item3”, Value=”3”});
绑定到ComboBox
cbo1.ItemsSource = lst;
cbo2.ItemsSource = lst;
dataGrid.DataContext = new TestObj(){
V1 = 1,
V2 = 2
};
xaml代码
<Grid x:Name="dataGrid">
<ComboBox x:Name="cbo1" DisplayMemberPath="Name" SelectedValuePath="Value" Width="80" IsSynchronizedWithCurrentItem="True" SelectedValue="{Binding Path=V1}"/>
<ComboBox x:Name="cbo2" DisplayMemberPath="Name" SelectedValuePath="Value" Width="80" IsSynchronizedWithCurrentItem="True" SelectedValue="{Binding Path=V2}"/>
</Grid>
测试看看, 结果竟然是cbo1和cbo2联动了
解决办法
cbo1.ItemsSource = lst.ToArray();
cbo2.ItemsSource = lst.ToArray();
dataGrid.DataContext = new TestObj(){
V1 = 1,
V2 = 2
};
ItemsSource 指向了同一个List导致select同步了, 通过ToArray()方法,其实clone了2个NameValue数组实例, 这样就不会产生select同步的.
相关文章推荐
- WPF的DataGrid绑定ItemsSource后第一次加载数据有个别列移位的解决办法
- WPF —ListView用ItemsSource绑定对象列表
- WPF学习笔记——ListBox用ItemsSource绑定数据源
- WPF (当 ItemsSource 正在使用时操作无效。改用 ItemsControl.ItemsSource 访问和修改)错误解决方案
- WPF学习笔记——ListBox用ItemsSource绑定数据源
- WPF学习笔记——ListBox用ItemsSource绑定数据源
- DevExpress WPF入门指南:跟随 Items Source 向导完成数据绑定
- 【WPF】当 ItemsSource 正在使用时操作无效。改用 ItemsControl.ItemsSource 访问和修改元素
- DevExpress WPF入门指南:跟随 Items Source 向导完成数据绑定
- WPF_DataGrid_ItemsSource绑定
- WPF 元素tag属性绑定一个属性或一个对象
- 如何从解决方案(Solution)或者工程(Project)去除与TFS Source Control的绑定关系?
- WPF ItemSource绑定的问题
- WPF一个绑定格式化显示的写法
- [WPF Bug清单]之(2)——RadioButton的IsChecked绑定失效
- WPF DataGrid绑定一个组合列
- WPF DataGrid 绑定BitmapSource
- Wpf ItemsControl 开启UI Virtualization 的条件
- MS Atlas(AJAX Control Toolkit)示例项目的一个小bug
- WPF 数据绑定 定制一个集合的视图