I used already Command Binding in my XAML but strangely this specififc one doesn't work. Did i miss something?
Class for Commands:
public class VMCommand : ICommand
{
public delegate void ICommandOnExecute(object parameter);
public delegate bool ICommandOnCanExecute(object parameter);
private ICommandOnExecute _execute;
private ICommandOnCanExecute _canExecute;
public VMCommand(Action exec, bool canExec)
{
_execute = delegate(object param) { exec(); };
_canExecute = delegate(object param) { return canExec; };
}
public VMCommand(Action<object> exec, bool canExec)
{
_execute = delegate(object param) { exec(param); };
_canExecute = delegate(object param) { return canExec; };
}
#region ICommand Members
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
return _canExecute.Invoke(parameter);
}
public void Execute(object parameter)
{
_execute.Invoke(parameter);
}
#endregion
}
Xaml:
<ListBox DataContext="{DynamicResource Client}" ItemsSource="{Binding Players}" Background="{StaticResource BrushDark}" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Name}" Foreground="{StaticResource BrushTextNormal}" Grid.Column="0" Grid.Row="0"/>
<TextBlock Text="{Binding KillsZombies}" Foreground="{StaticResource BrushTextNormal}" Grid.Column="1" Grid.Row="0"/>
<TextBlock Text="{Binding Score}" Foreground="{StaticResource BrushTextNormal}" Grid.Column="2" Grid.Row="0"/>
<TextBlock Text="{Binding Ping}" Foreground="{StaticResource BrushTextNormal}" Grid.Column="3" Grid.Row="0"/>
<StackPanel Orientation="Horizontal" Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="1" Name="Actions" Visibility="Collapsed">
<Button Content="Kick" Style="{StaticResource Button}" Margin="20,5,0,0" Width="50" Height="25" DataContext="{DynamicResource Client}" Command="{Binding Test}"/>
<!--{x:Static local:VMServer+PlayerCommand.KICK}-->
<Button Content="Ban" Style="{StaticResource Button}" Margin="20,5,0,0" Width="50" Height="25" Command="{Binding Test}"/>
</StackPanel>
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
<Setter Property="Visibility" Value="Visible" TargetName="Actions"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsFocused}" Value="False">
<Setter Property="Visibility" Value="Collapsed" TargetName="Actions"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Whereas in my VM:
private VMCommand _test;
public VMCommand Test
{
get
{
Action a = new Action(delegate()
{
MessageBox.Show("yay");
});
return new VMCommand(a, true);
}
}
In this case Client is my Viemodel here containing all the needed things.
Outside the listbox command binding seems to work like a charm (got a connect button with binding, works perfectly), but the buttons inside the Stackpanel wont fire any event at any time.
Did I miss something?
Aucun commentaire:
Enregistrer un commentaire