本文主要是介绍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>
个人记录,介绍并不完善,只有几个使用案例,仅供参考。
这篇关于WPF Interaction的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!