hello, it is necessary that the checkbox click command parameter be this checkbox itself and it was possible in the viewModel to read the checkbox Uid. as much as possible simplified the code ...

<ListBox ItemsSource="{Binding Collection}"> <ListBox.ItemTemplate> <DataTemplate> <CheckBox Command="{Binding CheckBoxCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}" Uid="{Binding Element.Id}"/> <!--ΠΏΡ€ΠΈΠ±ΠΈΠ½Π΄ΠΈΠ»ΠΈ ΠΊ полю id поля Element ΠΈΠ· структуры ΠΈΠ· ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… состоит Collection --> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

RelayCommand:

 public class RelayCommand<T> : ICommand { private readonly Action<T> _execute; private readonly Predicate<T> _canExecute; public event EventHandler CanExecuteChanged; public RelayCommand(Action<T> execute) : this(execute, null) { } public RelayCommand(Action<T> execute, Predicate<T> canExecute) { if (execute == null) throw new ArgumentNullException("ΠΏΡ€Π΅Π΄ΠΈΠΊΠ°Ρ‚ Π½Π΅ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π±Ρ‹Ρ‚ΡŒ Ρ€Π°Π²Π½Ρ‹ΠΌ Π½ΡƒΠ»ΡŽ"); _execute = execute; _canExecute = canExecute; } public bool CanExecute(object parameter) { return _canExecute == null ? true : _canExecute((T)parameter); } public void Execute(object parameter) { _execute((T)parameter); } public void RaiseCanExecuteChanged() { var handler = CanExecuteChanged; if (handler != null) { handler(this, EventArgs.Empty); } } } 

viewModel:

 class ViewModel { /// <summary> /// ΠΊΠΎΠΌΠ°Π½Π΄Π° Ρ‰Π΅Π»Ρ‡ΠΊΠ° ΠΏΠΎ checkbox /// </summary> private ICommand checkBoxCommand = null; public ICommand CheckBoxCommand { get { if (checkBoxCommand == null) { checkBoxCommand = new RelayCommand<CheckBox>(AddData); } return checkBoxCommand; } } private void AddData(CheckBox o) { var df = ((CheckBox)o).Uid; int brackPoint = 0; } /// <summary> /// конструктор /// </summary> public ViewModel() { } public ObservableCollection<Element> Collection { /**/ } 

}

Model (simplified):

 Struct Element { public ObservableCollection<someClass> Collection { /**/ } public int Id { /**/ }; } 

I put a bryak in addData and click on checkBox - it does not go into addData, but there are no errors ... help solve the problem

  • Try this: Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, Path=DataContext.CheckBoxCommand}" - user227049
  • CommandParameter you sure that you need to transfer the CheckBox ? Instead, it is better to send DataContext immediately: CommandParameter="{Binding}" , and then private void AddData(Element o) {var df = o.UId; int brackPoint = 0; } private void AddData(Element o) {var df = o.UId; int brackPoint = 0; } private void AddData(Element o) {var df = o.UId; int brackPoint = 0; } - user227049
  • omg ... helped add code from AncestorType from the first post, thanks - xperious

1 answer 1

Bind to window context by name. You redefined the context when you specified an ItemSource.

 <Window x:Name="Main" ... <CheckBox Command="{Binding DataContext.CheckBoxCommand, ElementName=Main}"... 

Council In order to preserve the isolation of the ViewModel from the View, I recommend passing in as the parameter not a CheckBox , but an Element

  <CheckBox Command="{Binding DataContext.CheckBoxCommand, ElementName=Main}" CommandParameter="{Binding}"... 

ViewModel:

 checkBoxCommand = new RelayCommand<Element>(AddData); private void AddData(Element e) { var df = e.Id; ... } 
  • in my case, the Uid cannot be in the properties of the viewModel. Because The uid binds to the field of the structure of which the observablecollection consists, which, in turn, is attached to the listbox via the itemssource ... fixed the question condition taking into account the reality - xperious
  • @xperious well, so your binding goes to the elements of this structure. And he is also looking for CheckBoxCommand there, and not in the ViewModel - RusArt
  • and how to be? for this.DataContext = viewModel; Worth - xperious
  • @xperious updated the answer - RusArt
  • This code will not work;) - user227049