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

时间:2014-10-20 23:46:26

标签: c# wpf events wpf-controls custom-controls

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

有关为何会发生这种情况的任何提示?

1 个答案:

答案 0 :(得分:1)

在WPF中,您只能绑定到DependencyProperty,在WPF中,Bottons有一个可以绑定的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!");
    }
}

在上面的示例中,单击按钮时将调用Execute中的MyClickCommand方法。

现在为了使用更友好,您可以使用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);
    }
}

示例:

相关问题