Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy this Button Command Binding is not working?
    text
    copied!<p>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.</p> <p>Here is the latest xaml:</p> <pre><code> &lt;ItemsControl Grid.Column="0" Grid.Row="1" ItemsSource="{Binding ServerList}" Margin="5"&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Border Background="LightGray"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;Button Style="{DynamicResource MetroCircleButtonStyle}" Content="{StaticResource appbar_monitor}" Command="{Binding VM.ServerSelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Controls:MetroWindow}}" CommandParameter="{Binding .}"&gt;&lt;/Button&gt; &lt;Label Content="{Binding .}" HorizontalAlignment="Center" VerticalAlignment="Center"&gt;&lt;/Label&gt; &lt;/StackPanel&gt; &lt;/Border&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; </code></pre> <p>Here is my ServerSelectedCommand in my ViewModel:</p> <pre><code> private ViewModelCommand _ServerSelectedCommand; public ViewModelCommand ServerSelectedCommand { get { if (_ServerSelectedCommand == null) { _ServerSelectedCommand = new ViewModelCommand( p =&gt; { SelectServer(p); }, p =&gt; true ); } return _ServerSelectedCommand; } set { _ServerSelectedCommand = value; } } private void SelectServer(object parameter) { } </code></pre> <p>ViewModelCommand class is like RelayCommand. Here it is:</p> <pre><code>public class ViewModelCommand : Observable, ICommand { public bool CanExecuteValue { get { return CanExecute(null); } } public ViewModelCommand( Action&lt;object&gt; executeAction, Predicate&lt;object&gt; canExecute) { if (executeAction == null) throw new ArgumentNullException("executeAction"); _executeAction = executeAction; _canExecute = canExecute; } private readonly Predicate&lt;object&gt; _canExecute; public bool CanExecute(object parameter) { return _canExecute == null ? true : _canExecute(parameter); } public event EventHandler CanExecuteChanged; public void OnCanExecuteChanged() { OnPropertyChanged(() =&gt; CanExecuteValue); if (CanExecuteChanged != null) CanExecuteChanged(this, EventArgs.Empty); } private readonly Action&lt;object&gt; _executeAction; public void Execute(object parameter) { _executeAction(parameter); } } </code></pre> <p>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.</p> <pre><code>Command="{Binding ServerSelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}}" Command="{Binding ServerSelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:ViewModel}}}" </code></pre> <p>This also doesn't provide anything!</p> <pre><code>Command="{Binding RelativeSource={RelativeSource AncestorType=Controls:MetroWindow}}" </code></pre> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload