简体   繁体   English

为什么此按钮命令绑定不起作用?

[英]Why this Button Command Binding is not working?

I did this like 50 times before. 我之前做过50次。 I really don't know why it is not working this time. 我真的不知道为什么这次没有用。 I have a WPF application and my only dependency is MahApps.Metro. 我有一个WPF应用程序,我唯一的依赖项是MahApps.Metro。 I'm using it's MetroWindow and Dynamic Style on my Button. 我在按钮上使用的是MetroWindow和动态样式。

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. ViewModelCommand类类似于RelayCommand。 Here it is: 这里是:

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. 因此,让我们回到第一个xaml,这是我尝试过的最新版本。 Here are the codes that I tried for problematic Button line. 这是我为有问题的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! 谢谢!

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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM