[WPF]万物皆可绑定

发布时间 2023-09-21 10:40:03作者: 月夜幽

最近一个项目有一个需求,需要在ComboBox中选择时,获得SelecetedValue值并执行一个方法,查了一下可以用Interactivity进行触发器重写,用一个事件来绑定命令。

但网上很多资料真的很坑,写了方法但是没提前提条件,需要在NuGet中安装一个Microsoft.Xaml.Behaviors.Wpf包来支持这个功能,然后在xaml中引用命令控件。

(1)NuGet上下载这个包

 

(2)xaml中引入命名空间

 1 xmlns:i="http://schemas.microsoft.com/xaml/behaviors" 

(3)写触发器

<ComboBox
  Width="190"
  Background="White"
  FontSize="14"
  ItemsSource="{Binding ProjectList}" 
  SelectedValue="{Binding SelectedProjectValue}">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
      <i:InvokeCommandAction Command="{Binding ProjectSelectedCommand}"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</ComboBox>

(4)业务逻辑写到ProjectSelectedCommand中。