0

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.

H.B.
  • 166,899
  • 29
  • 327
  • 400
Zwan
  • 632
  • 2
  • 6
  • 23
  • yeah seem same problem but got pain understand tried and still not firing – Zwan Jun 20 '16 at 14:57
  • 1
    Would not recommend passing `CanExecute` as a boolean, but rather as a `Func`, otherwise you cannot change the value easily later. Your command class also needs to provide a method that can be called to raise `CanExecuteChanged`, which needs to be fired when the `CanExecute` function may change in return value. – H.B. Jun 20 '16 at 16:10

1 Answers1

0

You are trying to bind VM commands in datatemplate. It cannot find this command, because it has different context. try to bind in that way

Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:TypeOfYourControlOrWindow}}, Path=DataContext.YourCommand}"
tym32167
  • 4,741
  • 2
  • 28
  • 32
  • i'm in a usercontrol ComputerView if it matter tried : Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:ComputerView}} not working – Zwan Jun 20 '16 at 15:09
  • for info an other cmd work outside of listview if it help – Zwan Jun 20 '16 at 15:11
  • you forgot command in the end Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:ComputerView}}, Path=DataContext.RemoveComputer}" – tym32167 Jun 20 '16 at 15:31