如何在单击时绑定到Button的ObjectDataProvider上执行方法?

时间:2015-01-14 22:37:38

标签: c# .net wpf xaml

我需要我的ObjectDataProvider方法在点击我的按钮时执行,而当时发生的任何事情都与2个方法参数绑定。但我得到一个立即运行时错误,错误如下。

这是我的XAML代码,其中包含我最后一个按钮的Click事件的运行时错误。

<Window x:Class="LoginNS.LoginWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:local="clr-namespace:LoginNS"
    Title="LoginWindow" WindowStyle="None" AllowsTransparency="True"
    WindowStartupLocation="CenterScreen" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="254" Width="438" Opacity="1" Background="{x:Null}" Foreground="#FFB10000">

<Window.Resources>

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

</Window.Resources>

<Grid Width="400" Height="200">
    <Rectangle x:Name="LoginRectangle" Height="200" HorizontalAlignment="Left" VerticalAlignment="Top" Stroke="#FF7E0000" Width="400" RadiusX="40" RadiusY="40" Fill="#DC840000" StrokeThickness="4" />
    <Button x:Name="ExitButton" Style="{StaticResource ExitButton}" Click="exitButtonMethod" />
    <TextBox x:Name="UsernameTextbox" HorizontalAlignment="Left" Height="30" Margin="64,69,0,0" FontSize="14" BorderBrush="#FF7E0000" BorderThickness="2" Background="{StaticResource LoginGradient}" Foreground="#FF7E0000" VerticalAlignment="Top" Width="273"
            Text="{Binding Source={StaticResource LoginConnectToDB},
            Path=MethodParameters[0],BindsDirectlyToSource=true}"/>
    <TextBox x:Name="PasswordTextbox" HorizontalAlignment="Left" Height="30" Margin="64,104,0,0" FontSize="14" BorderBrush="#FF7E0000" BorderThickness="2" Background="{StaticResource LoginGradient}" Foreground="#FF7E0000" VerticalAlignment="Top" Width="273"
             Text="{Binding Source={StaticResource LoginConnectToDB},
            Path=MethodParameters[1],BindsDirectlyToSource=true}"/>
    <Button x:Name="LoginButton" Style="{StaticResource LoginButton}" Click="{StaticResource LoginConnectToDB}"/>

</Grid>

</Window>
  

类型&#39; System.Windows.Markup.XamlParseException&#39;的第一次机会异常。发生在PresentationFramework.dll

中      

其他信息:&#39;设置属性&#39; System.Windows.Controls.Primitives.ButtonBase.Click&#39;抛出异常。&#39;行号&#39; 28&#39;和行位置&#39; 38&#39;。

1 个答案:

答案 0 :(得分:2)

Click是一个事件。它不能绑定到DataContext。 Click事件可以通过Click属性进行响应,但是映射到Window或Control上的函数而不是绑定的DataContext。

CommandDependencyProperty 可以(并且应该)绑定到ICommand对象。 DelegateCommand是一个非常有用(简单)的实现:

public class DelegateCommand : ICommand
{
    private readonly Action<object> _execute;

    public event EventHandler CanExecuteChanged;

    public DelegateCommand(Action<object> execute)
    {
        _execute = execute;
    }

    public override bool CanExecute(object parameter)
    {
       return true;
    }

    public override void Execute(object parameter)
    {
        _execute(parameter);
    }
}

完整示例:http://wpftutorial.net/DelegateCommand.html

在命令的执行中(或传递给DelegateCommand的方法中,您将执行数据刷新。

相关问题