首先让我把我的代码。
StockGroup EntityType
public partial class StockGroup
{
public StockGroup()
{
this.StockGroups = new HashSet<StockGroup>();
this.Stocks = new HashSet<Stock>();
}
public int ID { get; set; }
public string GroupName { get; set; }
public Nullable<int> ParentID { get; set; }
public Nullable<System.DateTime> CreatedOn { get; set; }
public Nullable<System.DateTime> ModifiedOn { get; set; }
public virtual ICollection<StockGroup> StockGroups { get; set; }
public virtual StockGroup Parent { get; set; }
public virtual ICollection<Stock> Stocks { get; set; }
public override string ToString() { return GroupName; }
public override bool Equals(object obj)
{
StockGroup stkGrp = obj as StockGroup;
if (stkGrp == null)
return false;
else
return ID.Equals(stkGrp.ID);
}
public override int GetHashCode()
{
return ID.GetHashCode();
}
}
ViewModel中的一个属性,它使用Caliburn.Micro绑定到ComboBox。
private IList<StockGroup> _groupParents;
public IList<StockGroup> GroupParents
{
get
{
return _groupParents;
}
set
{
_groupParents = value;
NotifyOfPropertyChange(() => GroupParents);
}
}
ComboBox XAML
<ComboBox Name="GroupParents" ToolTip="group parents"
Margin="5,0,5,5"
IsSynchronizedWithCurrentItem="True"
core:Message.Attach="[Event GotFocus]=[LoadGroupParents]"
IsEditable="True"
DisplayMemberPath="GroupName"
SelectedValuePath="ID"
IsReadOnly="False">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
直到这里一切都很好,combobox从数据库中获取所有数据。我在ComboBox中选择了第一条记录。 当我使用鼠标选择不同的ComboBox项目时,所选项目无法更改,它仍然在第一个记录中。 ComboBox选择适用于KeyDown但不适用于Mouse。
对于SelectedItem,我有一个名为SelectedGroupParent的属性,当我按鼠标更改时,其值会更改,但它不会显示在ComboBox TextBox中。
请建议一些解决方法。我一直都在努力,但它不起作用。甚至绑定到CollectionView也无法正常工作。
答案 0 :(得分:1)
那是我的坏事。
实际上,我正在GotFocus上重新加载ComboBox,这使得所选项目始终位于索引1。