简体   繁体   中英

C# CaliburnMicro: How can I automatically display a value in a combobox depending on which item is selected in a datagrid?

When a user selects a row from the Datagrid. I would like that item's colour to automatically be selected in the combo box. At the moment, the Combobox is unresponsive to Datagrid selections.

XAML:

<DataGrid x:Name="MyCollection" SelectedItem="{Binding MySelectedItem}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=ItemName}"/>
    </DataGrid.Columns>
</DataGrid>

<ComboBox ItemsSource="{Binding ItemColours}" SelectedItem="{Binding MySelectedItem.Colour}"/>
<TextBox Text="{Binding MySelectedItem.Colour}" isEnabled="False"/>

ViewModel:

public BindableCollection<Item> MyCollection { get; set; }

private Item_mySelectedItem;
public Item MySelectedItem
{
    get { return _mySelectedItem; }
    set
    {
        _mySelectedItem= value;
        NotifyOfPropertyChange(() => MySelectedItem);
    }
}

// Constructor
public Myclass()
{
    MyCollection = GetData();
}

I've checked, and the Combobox is definitely wired to SelectedItem.Colour, because the colour is also displayed in a text box elsewhere and that's updated when the Combobox selection is manually changed. I would like the Combobox to have the same functionality and responsiveness as the TextBox:

  • When an item on the datagrid is selected, the combobox displays the item's colour;
  • Manually selecting a different colour changes the SelectedItem.Colour value.
  • Ideally, the ComboBox selected item displays the new SelectedItem's colour even when isEnabled=False, in the same way the the TextBox value is updated even if it is disabled.

I've tried SelectedItem="{Binding SelectedBall.Colour, Mode=TwoWay}" , and I couldn't see any difference.

Thanks for your help.

104/5000 不知道你的ItemColours是什么数据,我觉得SelectedItem="{Binding myselecteditem.colour}"在ItemsSource中找不到对应的值,所以没有反应?

The problem was that the ComboBox was comparing two Colour objects to see whether it should display the colour. Since two instances of "Blue" for example, have different hashcodes, it would always return false. The solution is to override the Equals() and GetHashCode() methods of my Colour class.

public partial class Colour
{
    public override string ToString()
    {
        return Name;
    }

    public override bool Equals(object obj)
    {
        Colour otherColour;
        try
        {
            otherColour = (Colour)obj;
        }
        catch (Exception)
        { return false; }

        return (otherColour.Id == this.Id);
    }

    public override int GetHashCode()
    {
        return this.Id;
    }
}

Now, when two instances of the Colour class have the same ColourID, they will be considered equal. This means that the ComboBox will recognise that the SelectedItem.Colour is one of its Colours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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