[英]Click event of template of a Custom Control in WPF
我已经在WPF中创建了一个自定义ContentControl并将其应用于以下模板:
<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>
问题在于,BtClose不会调用自定义控件的代码背后声明的BtClose_Click方法,如下所示:
public void BtClose_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine("Test");
}
该错误非常普遍:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
有什么暗示为什么会发生吗?
在WPF中,您只能绑定到DependencyProperty
,在WPF中,Bottons具有可以绑定到的Command属性。 命令是MVVM处理WPF中事件的方法。
这是WPF中的Command绑定的快速示例。
XAML:
<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!");
}
}
在上面的示例中,当您单击按钮时,将调用MyClickCommand
的Execute
方法。
现在,为了使它更易于使用,您可以使用RelayCommand
实现,这允许将委托传递到Command实现中,这在大多数情况下是在WPF中使用命令的最简单方法。
例:
XAML:
<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);
}
}
例:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.