WPF MVVM后台绑定没有Command属性的控件,绑定任何事件事件

前言,我们在WPF开发中应用MVVM开发模式时,一般用来绑定Button 的Command命令,但是有很多控件MS没有给出Command命令,我们需要添加一个DLL来增加绑定时的属性。

下面来们来看一看。

本次测试有:TextBlock,Labe,TextBox三个 控件。

测试界面如下:

前台代码:

<Window x:Class="NoCommandBingding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="Test_TextBox" TargetType="{x:Type TextBox}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Border x:Name="border" Width="Auto" Height="Auto" BorderThickness="1" BorderBrush="#FF7F9DB9">
                            <Grid x:Name="grid" Background="#FFFFFF">
                                <ScrollViewer x:Name="PART_ContentHost" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock Background="LightBlue" Text="我是一个Textbook 点击我" TextAlignment="Center" Margin="10" >
            <i:Interaction.Triggers>
                            <i:EventTrigger EventName="MouseLeftButtonUp">
                                <i:InvokeCommandAction Command="{Binding TextBlockCommand}" CommandParameter="{Binding ElementName=lb}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
        </TextBlock>

        <Label Grid.Row="1" Background="LightYellow" Content="                                       我是一个lable 点击我"  Margin="10">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseLeftButtonUp">
                    <i:InvokeCommandAction Command="{Binding LabelCommand}" CommandParameter="{Binding ElementName=lb}" />
                </i:EventTrigger>
            </i:Interaction.Triggers> 
        </Label>
        
        <TextBox Grid.Row="2" Background="PaleVioletRed" Text="我是一个Textbox 点击我" TextAlignment="Center" Margin="10" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="GotFocus">
                    <i:InvokeCommandAction Command="{Binding TextBoxCommand}" CommandParameter="{Binding ElementName=lb}" />
                </i:EventTrigger>
            </i:Interaction.Triggers> 
        </TextBox>
    </Grid>
</Window>

加入一个程序集:


然后在前台文件引用:

有必要说明一下:

  xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
后面的像一个网络地 址的东西并不是去引用了一个网上的程序集,微软只是作为一个命名空间来存放程序集的引用,如此而已。

测试效果:

1.

2.

3.


最后奉上Demo

Demo下载



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值