Kishore Kumar
Kishore Kumar

Reputation: 12874

ComboBox Selection not Working in WPF with Mouse

First let me put my code.

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.

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>

Everything is fine till here and combobox gets all the data from database. I have the first record as selected in ComboBox. When I select a different ComboBox Item using Mouse, the selected item couldn't changes and it still in the first record. ComboBox selection works with KeyDown but not with Mouse.

For SelectedItem, I have property called SelectedGroupParent whose value changes when I changes by Mouse, but it not displayed in 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.

Upvotes: 0

Views: 1042

Answers (1)

Kishore Kumar
Kishore Kumar

Reputation: 12874

That was my bad.

Actually, I was reloading the ComboBox on GotFocus, which is making the selected item to be always at index 1.

Upvotes: 1

Related Questions