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>

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

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值