WPF中自定义控件模板的点击事件

4

我已经在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

有什么线索可以解释这是为什么吗?

你无法绑定事件处理程序,你需要创建一个命令并将按钮的Command属性绑定到该命令。 - sa_ddam213
1个回答

0
在WPF中,你只能绑定到一个DependencyProperty。在WPF中,按钮有一个Command属性,可以进行绑定。命令是MVVM处理WPF事件的方式。
这里是一个快速示例,展示如何在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 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实现,这允许将委托传递到命令实现中,在大多数情况下是使用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);
    }
}

例子:


1
你能展示一下如何将命令特别绑定到自定义控件吗? - Examath

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接