0

I am trying to get the bool value of the checkbox present in the Listview. I am binding to a bool public property "Assignm" in the view model. I tried the below binding pattern but the problem is if i select one checkbox it selects all checkboxes and vice versa. I think this is because relativesource is listview and it works on complete listview. I also tried changing the relative source to ListviewItem but that didn't trigger anything. Can someone help me please. Do i need to change something here ?

  <GridViewColumn.CellTemplate>
  <DataTemplate>
  <CheckBox  Tag="{Binding MU_Identifier}" IsChecked="{Binding DataContext.Assignm, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}">
 </CheckBox>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
3
  • The Assignm property should be in the item class (where MU_Identifier is). The Binding would be IsChecked="{Binding Assignm}". Commented Feb 23, 2018 at 13:59
  • But is it a good idea to add another property to my item class (Entity Model) even though this bool value is not stored in the Database. ? I have web client architecture application. @Clemens Commented Feb 23, 2018 at 14:03
  • A better practice is to create another model for this purpose, that is this model includes the property needed to be bound. Then create a mapping between the two models. Commented Feb 23, 2018 at 14:07

2 Answers 2

1

Because your binding for IsChecked property is Assignm property which seems to be one property of your view model.

If there is a boolean property named Assignm for the data model of the DataSource of ListView, then just change the binding like this: {Binding Assignm}, as Tag property does.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't have a bool property in my data model so i created a public bool property. But as others also suggested the same i think i will have to add a new bool property to my entity model and the worst part is this value won't be stored in the database. i am using this checkbox to select the MU_Identifier and assign it to the users.
0

All your items are bounded to a single property, so when one item changes a property in your context it changes on other items.

To provide correct work all your items from ItemsSource should have property IsChecked. Check this Example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.