Wpf 多么合适<;T>;影响combobox的行为
在操作组合框时,我发现了一个有线问题。Xaml看起来像这样Wpf 多么合适<;T>;影响combobox的行为,wpf,combobox,system.componentmodel,Wpf,Combobox,System.componentmodel,在操作组合框时,我发现了一个有线问题。Xaml看起来像这样 <ComboBox x:Name="cb" ItemsSource="{Binding MyEntity.Choices}" SelectedItem="{Binding MyEntity.Choice,Mode=TwoWay}" Height="25" HorizontalAlignment="Stretch"
<ComboBox x:Name="cb" ItemsSource="{Binding MyEntity.Choices}"
SelectedItem="{Binding MyEntity.Choice,Mode=TwoWay}"
Height="25"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"/>
现在,如果我在Shaft上实现IEquatable&然后将列表设置为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);
}
}
}
公共类轴:可满足
{
私人内部id;
私有字符串\u名称;
公共整数Id{
获取{return\u id;}
设置{
_id=值;
}
}
公共字符串名
{
获取{return\u name;}
设置
{
_名称=值;
}
}
公共重写字符串ToString()
{
返回_name;
}
公共布尔等于(轴)
{
System.Diagnostics.Debug.WriteLine(“从object.Equals调用”);
//轴=obj作为轴;
如果(空!=轴)
{
System.Diagnostics.Debug.WriteLine(“为“+this.Name+”调用的等于。与“+shaft.Name”相比);
}
其他的
{
System.Diagnostics.Debug.WriteLine(“为“+this+”调用的相等值。与“+shaft”相比);
}
返回底座。等于(轴);
}
}
}
这表明即使列表为空,组合框也不会释放绑定到Itemsource的对象。
直到我们实现IEquatable
知道为什么会这样吗?两种实现中单击按钮时的行为是相同的。如果在第二种情况下没有重写Object.Equals,您怎么知道没有调用它 至于不“释放”对象,这似乎是WPF的一个已知问题: 作为解决方法,您可以执行以下操作之一:
- 调用集合。清除方法,而不是将集合引用设置为null
- 使用ObservableCollection而不是Collection
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);
}
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);
}
}
}