C# 单击WPF中自定义控件模板的事件
我在WPF中创建了一个自定义ContentControl,并应用了以下模板:C# 单击WPF中自定义控件模板的事件,c#,wpf,events,wpf-controls,custom-controls,C#,Wpf,Events,Wpf Controls,Custom Controls,我在WPF中创建了一个自定义ContentControl,并应用了以下模板: <Style TargetType="{x:Type local:BdlUserControl}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:BdlUserControl">
<Style TargetType="{x:Type local:BdlUserControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:BdlUserControl">
<Grid x:Name="ContentGrid">
<Grid.RowDefinitions>
<RowDefinition Height="22"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="White">
<StackPanel HorizontalAlignment="Right">
<Button Content="Close" Width="50" Name="BtClose" Click="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BtClose_Click}"/>
</StackPanel>
</Grid>
<Grid Grid.Row="1">
<ContentPresenter Content="{TemplateBinding Content}" Margin="{TemplateBinding Padding}"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
错误非常普遍:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
关于为什么会发生这种情况的任何提示?在WPF中,您只能绑定到一个
DependencyProperty
,在WPF中,botton有一个可以绑定的命令属性。命令是处理WPF中事件的MVVM方法
下面是WPF中命令绑定的一个快速示例
Xaml:
在上述示例中,单击按钮时将调用MyClickCommand
中的Execute
方法
现在,为了让这更方便用户,您可以使用RelayCommand
实现,这允许将委托传递到命令实现中,在大多数情况下,这是在WPF中使用命令的最简单方法
例如:
Xaml:
代码:
公共部分类主窗口:窗口
{
公共主窗口()
{
MyCommand=新的RelayCommand(MyMethod);
初始化组件();
DataContext=this;
}
公共中继命令MyCommand{get;set;}
私有void MyMethod()
{
MessageBox.Show(“单击!”);
}
}
公共类中继命令:ICommand
{
只读操作_执行;
只读功能可执行;
public RelayCommand(Action execute):这个(execute,null){}
public RelayCommand(Action execute):这个(execute,null){}
公共RelayCommand(操作执行,函数执行)
{
if(execute==null)
抛出新的ArgumentNullException(“执行”);
_execute=p=>execute();
_canExecute=canExecute;
}
公共RelayCommand(操作执行,函数执行)
{
if(execute==null)
抛出新的ArgumentNullException(“执行”);
_执行=执行;
_canExecute=canExecute;
}
公共布尔CanExecute(对象参数)
{
return _canExecute==null?true:_canExecute();
}
公共事件事件处理程序CanExecuteChanged
{
添加{CommandManager.RequerySuggested+=value;}
删除{CommandManager.RequerySuggested-=value;}
}
public void Execute(对象参数)
{
_执行(参数);
}
}
示例:您无法绑定到事件处理程序,您需要创建一个命令并将Buttons命令属性绑定到该命令。
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="50" Width="100" >
<Grid>
<Button Content="Click" Command="{Binding MyCommand}" />
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
MyCommand = new MyClickCommand();
InitializeComponent();
DataContext = this;
}
public MyClickCommand MyCommand { get; set; }
}
public class MyClickCommand : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
MessageBox.Show("click!");
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="50" Width="100" >
<Grid>
<Button Content="Click" Command="{Binding MyCommand}" />
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
MyCommand = new RelayCommand(MyMethod);
InitializeComponent();
DataContext = this;
}
public RelayCommand MyCommand { get; set; }
private void MyMethod()
{
MessageBox.Show("Click!");
}
}
public class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Func<bool> _canExecute;
public RelayCommand(Action execute) : this(execute, null) { }
public RelayCommand(Action<object> execute) : this(execute, null) { }
public RelayCommand(Action execute, Func<bool> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = p => execute();
_canExecute = canExecute;
}
public RelayCommand(Action<object> execute, Func<bool> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute();
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
}