Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF MVVM - Update Dropdown When Clicked

I have a dropdown (ComboBox) that displays all the com ports available on a machine. Now, ports come and go when you connect and disconnect devices.

For performance reasons I don't want to keep calling System.IO.Ports.SerialPort.GetPortNames(), but rather just call that when the user clicks on the Combobox? Is this possible? Is there an MVVM approach to this problem?

like image 538
Zeus82 Avatar asked Sep 25 '14 14:09

Zeus82


1 Answers

Use InvokeCommandAction.

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

DropDownOpenedCommand is an ICommand property on your ViewModel.

<ComboBox>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="DropDownOpened">
            <i:InvokeCommandAction Command="{Binding DropDownOpenedCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>

Edit: obviously DropDownOpened not SelectionChanged, as Patrice commented.

like image 190
Mike Fuchs Avatar answered Oct 18 '22 12:10

Mike Fuchs