I have a list that load items from an observable collection ComputerList
<ListView x:Name="icTodoList" ItemsSource="{Binding ComputerList}" SelectedItem="{Binding SelectedComputer}" Grid.Column="3" SelectionChanged="icTodoList_SelectionChanged_1">
<ListView.ItemTemplate>
<DataTemplate>
<Border BorderBrush="{Binding borderColor }" BorderThickness="2" Margin="0,0,0,1">
<Grid Margin="2" Height="auto" Width="auto">
<Button Height="18" Command="{Binding RemoveComputer}" HorizontalAlignment="Right" ToolTipService.ShowDuration="60000" Margin="0,1,38,0" x:Name="button1_Copy" VerticalAlignment="Top" Width="25" FontSize="11" Foreground="#FF6BADF6" Content="" BorderBrush="#FF6BADF6" Grid.Column="8"/>
</Grid>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
my button click should remove the items from observableCollection so i did build Icommand interface this way (in short version).
class ComputerViewModel : ViewModelBase
{
CustomClass _customClass = new CustomClass();
public readonly ObservableCollection<Model.Model.ControleData> _ComputerList = new ObservableCollection<Model.Model.ControleData>();
public ObservableCollection<Model.Model.ControleData> ComputerList { get { return _ComputerList; } }
public ComputerViewModel()
{
ComputerList.Add(new Model.Model.ControleData { ComputerName = "TESTMACHINE", titlePing = "online",borderColor = Genkai.BlueMain });
// TextBoxText = "init";
_canExecute = true;
}
private ICommand _RemoveComputer;
public ICommand RemoveComputer
{
get
{
return _RemoveComputer ?? (_RemoveComputer = new CommandHandler(() => RemoveComp(), _canExecute));
}
}
private bool _canExecute;
public void RemoveComp()
{
Debug.WriteLine("close Item");
}
public class CommandHandler : ICommand
{
private Action _action;
private bool _canExecute;
public CommandHandler(Action action, bool canExecute)
{
_action = action;
_canExecute = canExecute;
}
public void Execute(object parameter)
{
_action();
}
public bool CanExecute(object parameter)
{
return _canExecute;
}
public event EventHandler CanExecuteChanged;
}
}
the action removecomp is not fired when i click.
but with this in my view model its fired
var hwc = new CommandHandler(RemoveComp,true);
if (hwc.CanExecute(this))
hwc.Execute(this);
so i guess i miss something in my WPF view.