wpf的命令怎么绑定多个条件
2个回答
展开全部
一般来说,条件不符合时,要禁用按钮。而不是等到执行时,再去判断条件。
WPF的ICommand模式,或者RoutedCommand模式都支持CanExecute回调。
你可以在CanExecute中判断条件。
比如下例,CanExecute判断名字输入框是否空白,并相应禁用按钮。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local ="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
<CommandBinding Command="{x:Static local:MainWindow.MyCommand}"
CanExecute="CanExecuteMyCommand"
Executed="ExecutetMyCommand" />
</Window.CommandBindings>
<StackPanel>
<Label>名字:</Label>
<TextBox Name="textbox1"/>
<Button Command="{x:Static local:MainWindow.MyCommand}" Content="提交" Margin="0 10" />
</StackPanel>
</Window>
using System;
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void CanExecuteMyCommand(object sender,CanExecuteRoutedEventArgs e)
{
e.CanExecute = !string.IsNullOrEmpty(this.textbox1.Text);
}
private void ExecutetMyCommand(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Hello " + this.textbox1.Text);
}
public static RoutedCommand MyCommand = new RoutedCommand();
}
}
WPF的ICommand模式,或者RoutedCommand模式都支持CanExecute回调。
你可以在CanExecute中判断条件。
比如下例,CanExecute判断名字输入框是否空白,并相应禁用按钮。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local ="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
<CommandBinding Command="{x:Static local:MainWindow.MyCommand}"
CanExecute="CanExecuteMyCommand"
Executed="ExecutetMyCommand" />
</Window.CommandBindings>
<StackPanel>
<Label>名字:</Label>
<TextBox Name="textbox1"/>
<Button Command="{x:Static local:MainWindow.MyCommand}" Content="提交" Margin="0 10" />
</StackPanel>
</Window>
using System;
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void CanExecuteMyCommand(object sender,CanExecuteRoutedEventArgs e)
{
e.CanExecute = !string.IsNullOrEmpty(this.textbox1.Text);
}
private void ExecutetMyCommand(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Hello " + this.textbox1.Text);
}
public static RoutedCommand MyCommand = new RoutedCommand();
}
}
2017-05-12
展开全部
不符合要禁用
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询