当前位置: 首页> 财经> 访谈 > wap网站建设学什么_免费咨询英文_百度关键词优化推广_正规营销培训

wap网站建设学什么_免费咨询英文_百度关键词优化推广_正规营销培训

时间:2025/9/7 18:54:45来源:https://blog.csdn.net/weixin_43626218/article/details/145933534 浏览次数:0次
wap网站建设学什么_免费咨询英文_百度关键词优化推广_正规营销培训

前言

C#WPF之命令绑定

内容

有些控件不支持直接绑定命令,可以调用其他依赖实现命令的绑定。

依赖:Microsoft.Xaml.Behaviors.Wpf

使用如下代码可以实现事件的命令绑定,及传递参数:

1、引用:xmlns:behavior=“http://schemas.microsoft.com/xaml/behaviors”
2、添加命令绑定代码( 如:代码①)
3、创建实现ICommand 的类public class EventsCommand : ICommand(如代码②)
4、创建命令 public ICommand LoadedWindowCommand{ get; set; }
5、初始化命令、并创建方法(如:代码③)

其中代码①写在**.xaml窗体视图中。代码②单独创建类。命令的初始化及方法绑定是创建在视图对应的ViewModel中如代码③。

代码①
<!--绑定上下文-->
<Window.DataContext><viewmodels:MainViewModel/>
</Window.DataContext><!--事件命令绑定-->
<behavior:Interaction.Triggers><!--窗体加载命令绑定--><behavior:EventTrigger EventName="Loaded"><behavior:InvokeCommandAction Command="{Binding LoadedWindowCommand}"   PassEventArgsToCommand="True"/></behavior:EventTrigger><!--窗体关闭命令绑定--><behavior:EventTrigger EventName="Closing"><behavior:InvokeCommandAction Command="{Binding ClosingWindowCommand}" PassEventArgsToCommand="True"/></behavior:EventTrigger>
</behavior:Interaction.Triggers>
代码②
public class EventsCommand<T> : ICommand
{private readonly Action<T> _execute;private readonly Func<T, bool> _canExecute;public EventsCommand(Action<T> execute, Func<T, bool> canExecute = null){_execute = execute ?? throw new ArgumentNullException(nameof(execute));_canExecute = canExecute;}public bool CanExecute(object parameter){return _canExecute?.Invoke((T)parameter) ?? true;}public void Execute(object parameter){_execute((T)parameter);}public event EventHandler CanExecuteChanged{add { CommandManager.RequerySuggested += value; }remove { CommandManager.RequerySuggested -= value; }}
}
代码③
public class MainViewModel:INotifyPropertyChanged
{public ICommand LoadedWindowCommand { get; set; }public ICommand ClosingWindowCommand { get; set; }public MainViewModel(){LoadedWindowCommand = new EventsCommand<object>(OnLoadedWindow);ClosingWindowCommand = new EventsCommand<object>(OnClosingWindow);}private void OnLoadedWindow(object e){}private void OnClosingWindow(object e){}
}

命令绑定例外

描述

有一种情况是控件里面添加控件,次数使用命令绑定传递参数。使用事件时,传递的一般是外层控件对象。

那么如何实现传递里面的对象呢,可以使用如下方法:使用DataContext数据上下文来传递。

<ScrollViewer Background="#AEAEAE" x:Name="RecordScrollViewer"><ListBox ItemsSource="{Binding ChatRecordCollection}" Margin="5"><ListBox.ItemTemplate><DataTemplate><!-- 显示消息内容 --><TextBlock Text="{Binding Data}"  Margin="10,0,0,0"><behavior:Interaction.Triggers><!--鼠标点击命令事件--><behavior:EventTrigger EventName="PreviewMouseDown"><behavior:InvokeCommandActionCommand="{Binding DataContext.ChatRecordMouseDownCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"CommandParameter="{Binding}"PassEventArgsToCommand="True"/></behavior:EventTrigger></behavior:Interaction.Triggers></TextBlock></DataTemplate></ListBox.ItemTemplate></ListBox>
</ScrollViewer>

结语

以上是个人日常学习中学到的一下知识,和理解。

关键字:wap网站建设学什么_免费咨询英文_百度关键词优化推广_正规营销培训

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: