WPF Interaction

使用 Interaction 里面的方法来将事件映射到ViewModel层

引用命名空间:System.Windows.Interactivity.dll

在xaml界面引用这两种任意一个就行,这两个做的事情是相同的。

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        Title="MainWindow" Height="350" Width="525">
        <!--xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"-->
    <!--页面加载命令-->
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding Page_Loaded}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    
    <i:Interaction.Behaviors>
        <c:WindowBehavior Close="{Binding ToClose}" />
    </i:Interaction.Behaviors>

    <Grid>
        <ComboBox x:Name="cb_kaname" IsReadOnly="False" BorderThickness="1"  BorderBrush="#999999" ItemsSource="{Binding lst_KaLei, Mode=TwoWay}" SelectedItem="{Binding Sel_Kalei, Mode=TwoWay}" Text="{Binding new_ka_name, Mode=TwoWay}" DisplayMemberPath="ka_name" Width="Auto" MinWidth="80" MaxWidth="200" IsEditable="True">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding cb_kaname_SelectionChanged}" CommandParameter="{Binding ElementName=cb_kaname}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ComboBox>
        <TextBox Name="tb_dazhebilvamount" Background="Transparent" Style="{StaticResource input_textbox}" MaxLength="3" HorizontalContentAlignment="Center" Text="{Binding new_faka_dazhebilv, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
            <intr:Interaction.Triggers>
                <intr:EventTrigger EventName="TextChanged">    
                    <intr:InvokeCommandAction Command="{Binding txt_TextChanged,TargetNullValue='has error'}" CommandParameter="tb_dazhebilvamount" ></intr:InvokeCommandAction>
                </intr:EventTrigger>
                <intr:EventTrigger EventName="GotFocus">
                    <intr:InvokeCommandAction Command="{Binding txt_GotFocus}"></intr:InvokeCommandAction>
                </intr:EventTrigger>
                <intr:EventTrigger EventName="LostFocus">
                    <intr:InvokeCommandAction Command="{Binding txt_LostFocus}"></intr:InvokeCommandAction>
                </intr:EventTrigger>
            </intr:Interaction.Triggers>
        </TextBox>      
        <Button x:Name="btnLogin" Content="登  录" Width="100" FontSize="20" Margin="10" Style="{StaticResource btnOK}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <i:InvokeCommandAction Command="{Binding btnLogin}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>  
    </Grid>
</Window>

个人记录,介绍并不完善,只有几个使用案例,仅供参考。

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-03-10 12:52:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-10 12:52:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-10 12:52:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-10 12:52:05       20 阅读

热门阅读

  1. 当Github启用PSA之后...

    2024-03-10 12:52:05       20 阅读
  2. 鸿蒙 进程模型-公共事件

    2024-03-10 12:52:05       20 阅读
  3. 解释 Git 的基本概念和使用方式

    2024-03-10 12:52:05       23 阅读
  4. VGG16-CF-VGG11实验报告

    2024-03-10 12:52:05       23 阅读
  5. vscode中开发goalng,debug时遇到的tools报错问题

    2024-03-10 12:52:05       23 阅读
  6. 用spark进行数据查询常用语法总结

    2024-03-10 12:52:05       24 阅读
  7. 数据仓库的主流分层架构

    2024-03-10 12:52:05       26 阅读
  8. 基于qt实现的类的序列化和反序列化

    2024-03-10 12:52:05       23 阅读
  9. 云原生技术实践:Kubernetes集群的部署与运维

    2024-03-10 12:52:05       19 阅读
  10. pytorch CV入门4-使用MobileNet解决视觉相关问题

    2024-03-10 12:52:05       19 阅读
  11. 【pytorch可视化工具】

    2024-03-10 12:52:05       24 阅读