wpf 为什么这个按钮命令绑定不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17420493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:07:36  来源:igfitidea点击:

Why this Button Command Binding is not working?

c#.netwpfsilverlightmahapps.metro

提问by Xelom

I did this like 50 times before. I really don't know why it is not working this time. I have a WPF application and my only dependency is MahApps.Metro. I'm using it's MetroWindow and Dynamic Style on my Button.

我以前这样做过 50 次。我真的不知道为什么这次不起作用。我有一个 WPF 应用程序,我唯一的依赖项是 MahApps.Metro。我在我的按钮上使用它的 MetroWindow 和 Dynamic Style。

Here is the latest xaml:

这是最新的xaml:

    <ItemsControl Grid.Column="0" Grid.Row="1" ItemsSource="{Binding ServerList}" Margin="5">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border Background="LightGray">
                    <StackPanel Orientation="Horizontal">
                        <Button Style="{DynamicResource MetroCircleButtonStyle}" Content="{StaticResource appbar_monitor}" Command="{Binding VM.ServerSelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Controls:MetroWindow}}" CommandParameter="{Binding .}"></Button>
                        <Label Content="{Binding .}" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

Here is my ServerSelectedCommand in my ViewModel:

这是我的 ViewModel 中的 ServerSelectedCommand:

    private ViewModelCommand _ServerSelectedCommand;
    public ViewModelCommand ServerSelectedCommand
    {
        get
        {
            if (_ServerSelectedCommand == null)
            {
                _ServerSelectedCommand = new ViewModelCommand(
                            p => { SelectServer(p); },
                            p => true
                    );
            }
            return _ServerSelectedCommand;
        }
        set { _ServerSelectedCommand = value; }
    }

    private void SelectServer(object parameter)
    {

    }

ViewModelCommand class is like RelayCommand. Here it is:

ViewModelCommand 类就像 RelayCommand。这里是:

public class ViewModelCommand : Observable, ICommand
{
    public bool CanExecuteValue
    {
        get { return CanExecute(null); }
    }
    public ViewModelCommand(
                Action<object> executeAction,
                Predicate<object> canExecute)
    {

        if (executeAction == null)
            throw new ArgumentNullException("executeAction");

        _executeAction = executeAction;
        _canExecute = canExecute;
    }
    private readonly Predicate<object> _canExecute;
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }
    public event EventHandler CanExecuteChanged;
    public void OnCanExecuteChanged()
    {
        OnPropertyChanged(() => CanExecuteValue);
        if (CanExecuteChanged != null)
            CanExecuteChanged(this, EventArgs.Empty);
    }
    private readonly Action<object> _executeAction;
    public void Execute(object parameter)
    {
        _executeAction(parameter);
    }
}

Sorry for a lot of code. But I need to add them in order to find the problem which I can't see. So lets turn back to first xaml, that is the latest one I tried. Here are the codes that I tried for problematic Button line.

抱歉有很多代码。但是我需要添加它们才能找到我看不到的问题。所以让我们回到第一个 xaml,这是我尝试过的最新一个。这是我为有问题的 Button 行尝试的代码。

Command="{Binding ServerSelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}}"

Command="{Binding ServerSelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:ViewModel}}}"

This also doesn't provide anything!

这也没有提供任何东西!

Command="{Binding RelativeSource={RelativeSource AncestorType=Controls:MetroWindow}}"

Thanks!

谢谢!

回答by Peter Holmes

This binding looks like it is looking for ServerSelectedCommand on the ItemsControl:

这个绑定看起来像是在 ItemsControl 上寻找 ServerSelectedCommand:

Command="{Binding ServerSelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}}"

try this instead:

试试这个:

Command="{Binding DataContext.ServerSelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}}"

Assuming of course that the DataContext of the ItemsControl is your ViewModel.

当然,假设 ItemsControl 的 DataContext 是您的 ViewModel。