【问题标题】:How to bind a SelectionChanged event of ComboBox in silverlight 4 using MVVM?如何使用 MVVM 在 silverlight 4 中绑定 ComboBox 的 SelectionChanged 事件?
【发布时间】:2012-04-12 08:09:27
【问题描述】:

我正在尝试使用 RoutingCommand 绑定 ComboBox 的 SelectionChanged 事件(与绑定按钮的命令相同)。但无法实现。我不喜欢使用任何第三方控件或在属性中编写代码。

【问题讨论】:

    标签: silverlight


    【解决方案1】:

    您可以使用System.Windows.Interactivity dll。

    代码将是这样的:

    <UserControl
     ...
     xmlns:ei="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
     ...>    
     ...
     <Combobox>
        <ei:Interaction.Triggers>
           <ei:EventTrigger EventName="SelectionChanged">
               <ei:InvokeCommandAction Command="{Binding Command}"/>
           </ei:EventTrigger>
        </ei:Interaction.Triggers>
     </Combobox>
     ...    
    </UserControl>
    

    【讨论】:

      【解决方案2】:

      您确定需要 SelectionChanged 事件吗?只需绑定

      SelectedItem="{Binding Path=ComboSelected, Mode=TwoWay}"
      

      其中 ComboSelected 是您的 ViewModel 的属性。并在 ComboSelected 属性的设置器中执行您需要的操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-29
        • 2014-06-27
        • 2014-03-26
        • 1970-01-01
        • 2010-10-31
        • 2010-12-21
        • 1970-01-01
        • 2011-10-05
        相关资源
        最近更新 更多