单击按钮时如何在绑定到 Button 的 ObjectDataProvider 上执行方法?
How do you execute a method on the ObjectDataProvider bound to a Button when it is clicked?
我需要我的 ObjectDataProvider
方法在单击我的按钮时执行,无论当时碰巧绑定到 2 个方法参数。但是我立即收到 运行 时间错误,错误如下。
这是我的 XAML 代码,在我最后一个按钮的点击事件中包含 运行 次错误。
<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>
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: 'Set property 'System.Windows.Controls.Primitives.ButtonBase.Click' threw an exception.' Line number '28' and line position '38'.
Click
是一个事件。它不能绑定到 DataContext。可以通过 Click
属性响应 Click 事件,但它映射到 Window 或 Control 上的函数而不是绑定的 DataContext。
Command
是一个 DependencyProperty
它 可以 (并且应该)绑定到 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
的方法中,您将执行数据刷新。
我需要我的 ObjectDataProvider
方法在单击我的按钮时执行,无论当时碰巧绑定到 2 个方法参数。但是我立即收到 运行 时间错误,错误如下。
这是我的 XAML 代码,在我最后一个按钮的点击事件中包含 运行 次错误。
<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>
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: 'Set property 'System.Windows.Controls.Primitives.ButtonBase.Click' threw an exception.' Line number '28' and line position '38'.
Click
是一个事件。它不能绑定到 DataContext。可以通过 Click
属性响应 Click 事件,但它映射到 Window 或 Control 上的函数而不是绑定的 DataContext。
Command
是一个 DependencyProperty
它 可以 (并且应该)绑定到 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
的方法中,您将执行数据刷新。