0

I want to know if I can get the Item source item from a checked change event in WPF?

XAML

                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding Active, Mode=TwoWay}" Checked="CheckBox_Checked" HorizontalAlignment="Center"></CheckBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>

Sample C# (on the lines of correct answer.. I hope)

     private void CheckBox_Checked(object sender, RoutedEventArgs e)
    {

        **// Its not e.source, most events it's e.item...but the checked event doesn't use this..**

        Customer c = e.Source as Customer;



        if(c != null)
        .....
    }

Thanks

user3428422
  • 4,300
  • 12
  • 55
  • 119
  • What do you want that for? [UI is NOT Data](http://stackoverflow.com/a/14382137/643085). If your checkbox is already Bound to an underlying object, why not just listen for the property change in the underlying object? what are you trying to do? – Federico Berasategui Apr 22 '14 at 15:27
  • Because the underlying object is from a ObservableCollection and I want to listen out for individual property events i.e. when the active checkbox is checked which the collection won't allow (easily) but I also need the row data as well – user3428422 Apr 22 '14 at 15:32
  • the UI is not the right place to put whatever business logic you need. Either listen for the `PropertyChanged` event in your data item, or put your logic in a delegate and call that in the setter of the `Active` property. Or use something like a [`Selectable`](http://stackoverflow.com/a/14971905/643085) approach. – Federico Berasategui Apr 22 '14 at 15:34
  • I agree that the logic needs to go into the ViewModel, but he selected wont work as it only selects the row on first click of the row and not on the checkbox. Good idea with the delegate, but I will be setting this in the model.. not the viewmodel.. again incorrect use of MVVM? – user3428422 Apr 22 '14 at 15:37

2 Answers2

0

Something like this, Assuming you are not following MVVM

  private void CheckBox_Checked(object sender, RoutedEventArgs e)
    {
      bool IsActive = (sender as CheckBox).IsChecked;
      if(IsActive)
      {
         Active;
      }
    }
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • I am following MVVM but I have realised that getting the individual property inside a ObservableCollection in a ViewModel isnt easy and will take a lot a code to do what I need. so this is a quick (but messy substitute) will have a look – user3428422 Apr 22 '14 at 15:47
0

you can do it like Refernece

Reference 2

Viewmodel

private bool _isactive;

    public bool IsActive
    {
        get { return _isactive; }
        set
        {
            _isactive = value;
            RaisePropertyChanged(() => IsActive);
        }
    }

if you want get the eventargs in mvvm . use Relay Command or Icommand

xaml

<CheckBox x:Name="chkbox" Command="{Binding CommandName}"
                      CommandParameter="{parameter}"/>

handle the command do necessary action.

Eldho
  • 7,795
  • 5
  • 40
  • 77
  • This syntax makes sense and how I would want to do it, but the property _isactive wouldn't be part of the current DataContext. So basically, not part of the collection. So to do the above, I would need to use RelativeSource I guess? – user3428422 Apr 23 '14 at 07:20
  • ya use relative source if source binding not part of current datacontext. Try the reference will help . or post more code i will help in u way i can – Eldho Apr 23 '14 at 16:50