时间:2016年10月24日 | 作者 : aaronyang | 分类 : AY梦 | 浏览: 2096次 | 评论 0 人
DEMO
前台
<TextBlock Text="Attached Command Behaviors aka ACB" FontWeight="Bold" FontSize="16"/> <Border CornerRadius="5" Margin="10" BorderBrush="Silver" BorderThickness="1" Grid.Row="1" TextElement.FontSize="14"> <StackPanel> <TextBlock Text="List of the last commands executed: " FontWeight="Bold"/> <ListBox ItemsSource="{Binding Messages}" Height="110" /> <Button Content="Clear" Command="{Binding ClearMessagesCommand}"/> </StackPanel> </Border> <StackPanel Grid.Row="2" > <WrapPanel Margin="0,10,0,20"> <TextBlock FontSize="14" Text="Border hooked to MouseDown event" Width="410"/> <Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" local:CommandBehavior.Event="MouseDown" local:CommandBehavior.Command="{Binding SomeCommand}" local:CommandBehavior.CommandParameter="MouseDown" > <TextBlock Text="MouseDown on this border to execute the command"/> </Border> </WrapPanel> <WrapPanel Margin="0,0,0,20"> <TextBlock FontSize="14" Text="Border hooked to the event selected in the ComboBox" Width="410"/> <Border Background="YellowGreen" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" local:CommandBehavior.Event="{Binding ElementName=events1, Path=SelectedItem}" local:CommandBehavior.Command="{Binding SomeCommand}" local:CommandBehavior.CommandParameter="{Binding ElementName=events1, Path=SelectedItem}" > <WrapPanel> <TextBlock Text="{Binding ElementName=events1, Path=SelectedItem}"/> <TextBlock Text=" on this border to execute the command"/> </WrapPanel> </Border> <ComboBox ItemsSource="{Binding Events}" MinWidth="200" SelectedIndex="1" x:Name="events1"/> </WrapPanel> <WrapPanel Margin="0,0,0,20"> <TextBlock FontSize="14" Text="Border hooked to an Action instead of a Command" Width="410" /> <Border Background="DarkSalmon" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" local:CommandBehavior.Event="MouseDown" local:CommandBehavior.Action="{Binding DoSomething}" local:CommandBehavior.CommandParameter="from the DarkSalmon Border :P"/> </WrapPanel> <WrapPanel Margin="0,10,0,20"> <TextBlock FontSize="14" Text="Border hooked to MouseDown event" Width="410"/> <Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" x:Name="test"> <local:CommandBehaviorCollection.Behaviors> <local:BehaviorBinding Event="MouseLeftButtonDown" Action="{Binding DoSomething}" CommandParameter="An Action on MouseLeftButtonDown"/> <local:BehaviorBinding Event="MouseRightButtonDown" Command="{Binding SomeCommand}" CommandParameter="A Command on MouseRightButtonDown"/> </local:CommandBehaviorCollection.Behaviors> <TextBlock Text="MouseDown on this border to execute the command"/> </Border> </WrapPanel> </StackPanel>
后台
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Input; using System.ComponentModel; using System.Collections.ObjectModel; using AttachedCommandBehavior; namespace AttachedCommandBehaviorDemo { public class DemoViewModel { /// <summary> /// Gets the list of events to bind to /// </summary> public IList<string> Events { get; private set; } /// <summary> /// Gets the list of Messages populated (The messages are the names of the events that execute the commands) /// </summary> public IList<string> Messages { get; private set; } /// <summary> /// Gets an action that adds a message /// </summary> public Action<object> DoSomething { get; private set; } /// <summary> /// Command that clears the list of messages /// </summary> public ICommand ClearMessagesCommand { get; private set; } /// <summary> /// Command that write the event name that executed the command /// </summary> public ICommand SomeCommand { get; private set; } public DemoViewModel() { DoSomething = x => Messages.Add("Action executed: " + x.ToString()); Messages = new ObservableCollection<string>(); Events = new[] { "PreviewMouseDown", "PreviewMouseUp", "PreviewMouseLeftButtonDown", "PreviewMouseLeftButtonUp", "PreviewMouseRightButtonDown", "PreviewMouseRightButtonUp", "MouseEnter", "MouseLeave" }; SomeCommand = new SimpleCommand { //this will set the Message property to the value of the CommandParameter ExecuteDelegate = x => Messages.Add(x.ToString()) }; ClearMessagesCommand = new SimpleCommand { ExecuteDelegate = x => Messages.Clear(), CanExecuteDelegate = x => Messages.Count > 0 }; DoSomething = x => Messages.Add("Action executed: " + x.ToString()); } } }
推荐您阅读更多有关于“AYUI,”的文章
合肥科大智能常年招聘.NET,Web前端,有想换工作的私聊我和AY交流
抖音号:wpfui,可以看到我的很多作品效果,私活合作请qq联系我
AYUI8社区版Github地址:前往获取
作者:杨洋(AaronYang简称AY,安徽六安人)目前是个人,还没公司和AY交流
高中学历,2015年1月17日开始,兴趣学习研究WPF,目前工作繁忙,不接任何活
声明:AYUI7个人与商用免费,源码可购买。部分DEMO不免费.AY主要靠卖技术服务挣钱
不是从我处购买的ayui7源码,我不提供任何技术服务,如果你举报从哪里买的,我可以帮你转正为我的客户,并送demo
查看捐赠AYUI7.X MVC教程 更新如下:
第一课 第二课 程序加密教程vs2015 企业版密钥HM6NR-QXX7C-DFW2Y-8B82K-WTYJV
vs2017 企业版密钥NJVYC-BMHX2-G77MM-4XJMR-6Q8QF
额 本文暂时没人评论 来添加一个吧
发表评论