How to add selectionchanged/dropdownopened to combobox via ICommand/EventTrigger in MVVP WPF

huangapple go评论57阅读模式
英文:

How to add selectionchanged/dropdownopened to combobox via ICommand/EventTrigger in MVVP WPF

问题

我正在使用MVVM模式构建一个程序。我可以在xaml.cs文件中的非MVVM方式处理selectionchanged事件的方法。但是我不知道在MVVM中如何做到这一点。

目前,我的所有ICommand属性都在viewmodel.cs文件中,视图代码如下:

<StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10,0" HorizontalAlignment="Right">
    <Label Content="Ports" Margin="2" />
    <ComboBox IsReadOnly="True" AllowDrop="True" x:Name="ComboBox_SerialPort" MinWidth="60" Margin="5" 
            ItemsSource="{Binding SerialPortList}" SelectedValue="{Binding SelectedSerialPort}"
            SelectionChanged="{Binding Cmd_ComboBox_SerialDropDownSelectionChanged}">
    </ComboBox>
</StackPanel>

显然,直接将ICommand属性绑定到SelectionChanged不起作用。但是ComboBox控件中没有Command属性。那么,我如何将ICommand附加到控件事件中呢?

谢谢

英文:

I'm building a program with MVVM pattern
I can handle selectionchanged event with method in xaml.cs file in non-MVVM way. but don't know how to do this in MVVM.

Currently all my ICommand properties are in viewmodel.cs file, and view code like below

&lt;StackPanel Grid.Row=&quot;0&quot; Orientation=&quot;Horizontal&quot; Margin=&quot;10,0&quot; HorizontalAlignment=&quot;Right&quot;&gt;
    &lt;Label Content=&quot;Ports&quot; Margin=&quot;2&quot; /&gt;
    &lt;ComboBox IsReadOnly=&quot;True&quot; AllowDrop=&quot;True&quot; x:Name=&quot;ComboBox_SerialPort&quot; MinWidth=&quot;60&quot; Margin=&quot;5&quot; 
            ItemsSource=&quot;{Binding SerialPortList}&quot; SelectedValue=&quot;{Binding SelectedSerialPort}&quot;
            SelectionChanged=&quot;{Binding Cmd_ComboBox_SerialDropDownSelectionChanged}&quot; &gt;
    &lt;/ComboBox&gt;
&lt;/StackPanel&gt;

Apparently directly binding ICommand property to SelectionChanged is not working. but there's no Command property in Combobox control. So how do I attach ICommand to control events

Thanks

答案1

得分: 1

你可以在 SelectedSerialPort 源属性的 setter 中处理你的选择更改逻辑,该属性会在你在 ComboBox 中选择一个值时被设置。

英文:

You could handle your selection changed logic in the setter of the SelectedSerialPort source property which will get set when you select a value in the ComboBox.

答案2

得分: 0

Option 1:

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

Option 2:

<ComboBox SelectedValue="{Binding SelectedSerialPort, UpdateSourceTrigger=PropertyChanged}"/>
英文:

Option 1:

&lt;ComboBox&gt;
  &lt;i:Interaction.Triggers&gt;
        &lt;i:EventTrigger EventName=&quot;SelectionChanged&quot;&gt;
          &lt;i:InvokeCommandAction Command=&quot;{Binding YourCommandName}&quot;/&gt;
     &lt;/i:EventTrigger&gt;
  &lt;/i:Interaction.Triggers&gt;
&lt;/Combobox&gt;

Option 2:

&lt;ComboBox SelectedValue=&quot;{Binding SelectedSerialPort, UpdateSourceTrigger=PropertyChanged}&quot;/&gt; 

huangapple
  • 本文由 发表于 2023年4月4日 17:28:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定