简体   繁体   English

使用鼠标在WPF中无法使用ComboBox选择

[英]ComboBox Selection not Working in WPF with Mouse

First let me put my code. 首先让我输入我的代码。

StockGroup EntityType 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();
    }
}   

A Property from ViewModel which binds to ComboBox using Caliburn.Micro. ViewModel中的一个属性,该属性使用Caliburn.Micro绑定到ComboBox。

private IList<StockGroup> _groupParents;
public IList<StockGroup> GroupParents
{
    get
    {
        return _groupParents;
    }
    set
    {
        _groupParents = value;
        NotifyOfPropertyChange(() => GroupParents);
    }
}

ComboBox XAML 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>

Everything is fine till here and combobox gets all the data from database. 到这里一切都很好,并且combobox从数据库中获取所有数据。 I have the first record as selected in ComboBox. 我在ComboBox中选择了第一条记录。 When I select a different ComboBox Item using Mouse, the selected item couldn't changes and it still in the first record. 当我使用鼠标选择其他ComboBox项时,所选项无法更改,并且仍在第一条记录中。 ComboBox selection works with KeyDown but not with Mouse. ComboBox选择适用于KeyDown,但不适用于Mouse。

For SelectedItem, I have property called SelectedGroupParent whose value changes when I changes by Mouse, but it not displayed in ComboBox TextBox. 对于SelectedItem,我有一个称为SelectedGroupParent的属性,该属性的值在通过鼠标更改时会更改,但不会显示在ComboBox TextBox中。

Please suggest some fix to this. 请提出一些解决办法。 I have tried all the way, but its not working. 我一直尝试,但是没有用。 Even binding to CollectionView is not working. 甚至无法绑定到CollectionView。

That was my bad. 那是我的坏事。

Actually, I was reloading the ComboBox on GotFocus, which is making the selected item to be always at index 1. 实际上,我在GotFocus上重新加载了ComboBox,这使所选项目始终位于索引1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM