IEQuatable如何实现影响组合框的行为
在研究组合框时,我发现了一个有线问题。 Xaml 看起来像这样
<ComboBox x:Name="cb" ItemsSource="{Binding MyEntity.Choices}"
SelectedItem="{Binding MyEntity.Choice,Mode=TwoWay}"
Height="25"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"/>
假设 Itemsource 与 Shaft(对象)列表和 Shaft(对象)列表绑定。 selectedItem 是列表之一。
public partial class MainWindow : Window
{
private ShaftsData shaftData;
public ShaftsData ShaftData
{
get { return shaftData; }
set { shaftData = value; }
}
public MainWindow()
{
ShaftData = new ShaftsData();
InitializeComponent();
txtBox.Text = "Default";
this.DataContext = this;
SetComboCollectionAndSelectedShaft();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
shaftData.ComboCollection = null;
}
private void SetComboCollectionAndSelectedShaft()
{
Collection<Shaft> myCollection = new Collection<Shaft>();
for (int i = 1; i <= 5; ++i)
{
Shaft sh = new Shaft();
sh.Id = i;
sh.Name = string.Format("{0} {1} ", txtBox.Text, i);
myCollection.Add(sh);
}
shaftData.ComboCollection = myCollection;
shaftData.SelectedShaft = shaftData.ComboCollection[0];
}
}
public class ShaftsData : INotifyPropertyChanged
{
private Collection<Shaft> _comboCollection;
private Shaft _selectedShaft;
public Collection<Shaft> ComboCollection
{
get
{
return _comboCollection;
}
set
{
_comboCollection = value;
OnPropertyChanged("ComboCollection");
}
}
public Shaft SelectedShaft
{
get { return _selectedShaft; }
set
{
_selectedShaft = value;
OnPropertyChanged("SelectedShaft");
}
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
然后我尝试将此列表设置为空(请参阅Button_Click)。Combobox 正在调用List & 的每个对象的.Equals。比较最后选定的对象。 虽然我的期望是它不应该调用 .equals &将 SelectedItem 设置为 null。
public class Shaft
{
private int _id;
private string _name;
public int Id {
get { return _id; }
set {
_id = value;
}
}
public string Name
{
get { return _name; }
set
{
_name = value;
}
}
public override string ToString()
{
return _name;
}
public override bool Equals(object obj)
{
System.Diagnostics.Debug.WriteLine("Calling from object.Equals");
Shaft shaft = obj as Shaft;
if (null != shaft)
{
System.Diagnostics.Debug.WriteLine("Equals called for " + this.Name + ". Compared with " + shaft.Name);
}
else
{
System.Diagnostics.Debug.WriteLine("Equals called for " + this + ". Compared with " + shaft);
}
return base.Equals(obj);
}
现在,如果我在 Shaft & 上实现 IEquatable然后将 List 设置为 null 就可以正常工作了。意味着没有电话会去 .Eqauls & selectedItem 设置为 null。
新实现
public class Shaft : IEquatable<Shaft>
{
private int _id;
private string _name;
public int Id {
get { return _id; }
set {
_id = value;
}
}
public string Name
{
get { return _name; }
set
{
_name = value;
}
}
public override string ToString()
{
return _name;
}
public bool Equals(Shaft shaft)
{
System.Diagnostics.Debug.WriteLine("Calling from object.Equals");
// Shaft shaft = obj as Shaft;
if (null != shaft)
{
System.Diagnostics.Debug.WriteLine("Equals called for " + this.Name + ". Compared with " + shaft.Name);
}
else
{
System.Diagnostics.Debug.WriteLine("Equals called for " + this + ". Compared with " + shaft);
}
return base.Equals(shaft);
}
}
}
这表明即使列表为空,组合框也不会释放绑定到 Itemsource 的对象。 直到我们实现 IEquatable 。
知道为什么会这样吗?
While working on combo box I found out a wired issue. Xaml looks like this
<ComboBox x:Name="cb" ItemsSource="{Binding MyEntity.Choices}"
SelectedItem="{Binding MyEntity.Choice,Mode=TwoWay}"
Height="25"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"/>
Let’s say Itemsource is binded with a List of Shaft (object) & selectedItem is one among the list.
public partial class MainWindow : Window
{
private ShaftsData shaftData;
public ShaftsData ShaftData
{
get { return shaftData; }
set { shaftData = value; }
}
public MainWindow()
{
ShaftData = new ShaftsData();
InitializeComponent();
txtBox.Text = "Default";
this.DataContext = this;
SetComboCollectionAndSelectedShaft();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
shaftData.ComboCollection = null;
}
private void SetComboCollectionAndSelectedShaft()
{
Collection<Shaft> myCollection = new Collection<Shaft>();
for (int i = 1; i <= 5; ++i)
{
Shaft sh = new Shaft();
sh.Id = i;
sh.Name = string.Format("{0} {1} ", txtBox.Text, i);
myCollection.Add(sh);
}
shaftData.ComboCollection = myCollection;
shaftData.SelectedShaft = shaftData.ComboCollection[0];
}
}
public class ShaftsData : INotifyPropertyChanged
{
private Collection<Shaft> _comboCollection;
private Shaft _selectedShaft;
public Collection<Shaft> ComboCollection
{
get
{
return _comboCollection;
}
set
{
_comboCollection = value;
OnPropertyChanged("ComboCollection");
}
}
public Shaft SelectedShaft
{
get { return _selectedShaft; }
set
{
_selectedShaft = value;
OnPropertyChanged("SelectedShaft");
}
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
And then I tried to make this list to null (Please refer to Button_Click) .Combobox is calling the .Equals of each object of the List & compare the last Selected object.
While my expectation is it should not call .equals & set the SelectedItem to null.
public class Shaft
{
private int _id;
private string _name;
public int Id {
get { return _id; }
set {
_id = value;
}
}
public string Name
{
get { return _name; }
set
{
_name = value;
}
}
public override string ToString()
{
return _name;
}
public override bool Equals(object obj)
{
System.Diagnostics.Debug.WriteLine("Calling from object.Equals");
Shaft shaft = obj as Shaft;
if (null != shaft)
{
System.Diagnostics.Debug.WriteLine("Equals called for " + this.Name + ". Compared with " + shaft.Name);
}
else
{
System.Diagnostics.Debug.WriteLine("Equals called for " + this + ". Compared with " + shaft);
}
return base.Equals(obj);
}
Now if I implement IEquatable on Shaft & then set the List to null it works fine. Means no call is going to .Eqauls & selectedItem is set to null.
New Implementation
public class Shaft : IEquatable<Shaft>
{
private int _id;
private string _name;
public int Id {
get { return _id; }
set {
_id = value;
}
}
public string Name
{
get { return _name; }
set
{
_name = value;
}
}
public override string ToString()
{
return _name;
}
public bool Equals(Shaft shaft)
{
System.Diagnostics.Debug.WriteLine("Calling from object.Equals");
// Shaft shaft = obj as Shaft;
if (null != shaft)
{
System.Diagnostics.Debug.WriteLine("Equals called for " + this.Name + ". Compared with " + shaft.Name);
}
else
{
System.Diagnostics.Debug.WriteLine("Equals called for " + this + ". Compared with " + shaft);
}
return base.Equals(shaft);
}
}
}
This shows that the combobox is not releasing the objects binded to Itemsource even if the list is null.
Until we implements IEquatable .
Any idea why it is like that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于两种实现,Button_Click 的行为是相同的。如果你没有在第二种情况下重写它,你怎么知道没有调用 Object.Equals 呢?
至于不“释放”对象,这似乎是 WPF 的一个已知问题:http://support.microsoft。 com/kb/938416
作为解决方法,您可以执行以下操作之一:
The behaviour upon Button_Click is identical for both implementations. How did you know there were no calls to Object.Equals if you didn't override it in the second case?
As for not "releasing" objects this looks to be a known issue of WPF: http://support.microsoft.com/kb/938416
As a workaround you can do one of the following: