2013-07-02 122 views
1

我之前做了50次。我真的不知道为什么这次不工作。我有一个WPF应用程序,我唯一的依赖是MahApps.Metro。我在我的按钮上使用MetroWindow和Dynamic Style。为什么这个按钮命令绑定不起作用?

下面是最新的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> 

这是我在我的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类就像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); 
    } 
} 

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

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

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

这也不提供任何东西!

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

谢谢!

+0

我在使用扩展WPF工具包和MahApps.Metro时面临类似的问题。你能否删除'MahApps.Metro'并检查命令是否正常工作? –

+0

Snoop能与您的应用程序一起工作吗?所以你可以在运行时检查你的DataContext和Binding – blindmeis

+0

我现在没有使用MahApps尝试了它。将MetroWindow更改为窗口。它不工作。 @blindmeis当我尝试在xaml绑定中设置一个断点时,它说:“断点当前不会被命中,没有可执行代码与此行关联。” – Xelom

回答

5

这种结合看起来是寻找ServerSelectedCommand上的ItemsControl:

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

试试这个:

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

假设当然是ItemsControl的的DataContext的是你的ViewModel的。

+0

在我的第一个例子中,你可以看到VM.ServerSelectedCommand。这不起作用。 – Xelom

+0

VM.ServerSelectedCommand绝对不会工作。项目控件中的项目不会继承它们的父级datacontext,因此该按钮没有“VM”标识符的概念。我已经使用了Peter Holmes在几十个项目中提供的答案。检查您的输出窗口是否有任何绑定错误消息并为我们提供。 – Backlash