Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF命令参数绑定未更新_Wpf_Binding_Command_Commandparameter - Fatal编程技术网

WPF命令参数绑定未更新

WPF命令参数绑定未更新,wpf,binding,command,commandparameter,Wpf,Binding,Command,Commandparameter,我试图在WPF应用程序中使用带有按钮的命令和命令参数绑定。我有完全相同的代码在Silverlight中工作得很好,所以我想知道我做错了什么 我有一个组合框和一个按钮,其中命令参数绑定到组合框SelectedItem: <Window x:Class="WPFCommandBindingProblem.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="h

我试图在WPF应用程序中使用带有按钮的命令和命令参数绑定。我有完全相同的代码在Silverlight中工作得很好,所以我想知道我做错了什么

我有一个组合框和一个按钮,其中命令参数绑定到组合框SelectedItem:

<Window x:Class="WPFCommandBindingProblem.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <StackPanel Orientation="Horizontal">
        <ComboBox x:Name="combo" VerticalAlignment="Top" />
        <Button Content="Do Something" Command="{Binding Path=TestCommand}"
                CommandParameter="{Binding Path=SelectedItem, ElementName=combo}"
                VerticalAlignment="Top"/>        
    </StackPanel>
</Window>

背后的代码如下:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        combo.ItemsSource = new List<string>(){
            "One", "Two", "Three", "Four", "Five"
        };

        this.DataContext = this;

    }

    public TestCommand TestCommand
    {
        get
        {
            return new TestCommand();
        }
    }

}

public class TestCommand : ICommand
{
    public bool CanExecute(object parameter)
    {
        return parameter is string && (string)parameter != "Two";
    }

    public void Execute(object parameter)
    {
        MessageBox.Show(parameter as string);
    }

    public event EventHandler CanExecuteChanged;

}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
combo.ItemsSource=新列表(){
“一”、“二”、“三”、“四”、“五”
};
this.DataContext=this;
}
公共TestCommand TestCommand
{
得到
{
返回新的TestCommand();
}
}
}
公共类TestCommand:ICommand
{
公共布尔CanExecute(对象参数)
{
返回参数为string&(string)参数!=“两”;
}
public void Execute(对象参数)
{
MessageBox.Show(参数为字符串);
}
公共事件处理程序CanExecuteChanged;
}
对于我的Silverlight应用程序,随着组合框的SelectedItem的更改,CommandParameter绑定会导致使用当前选定项重新计算我的命令的CanExecute方法,并且相应地更新按钮启用状态

对于WPF,由于某些原因,只有在解析XAML时创建绑定时才会调用CanExecute方法


有什么想法吗?

您需要告诉WPF CanExecute可以更改-您可以在TestCommand类中自动执行此操作,如下所示:

public event EventHandler CanExecuteChanged
{
    add{CommandManager.RequerySuggested += value;}
    remove{CommandManager.RequerySuggested -= value;}
}

然后,每次视图中的属性更改时,WPF都会要求CanExecute。

这将重新评估UI上所有更改属性的所有命令?没有办法将其与委托命令一起使用?在silverlight中没有更简单或ootb的解决方案吗?这只是最简单的方法-您可以控制何时调用CanExecuteChanged事件-在这里,我只是将它设置为在框架决定它可能已更新时更新。