I'm trying to work with a datagrid using the MVVM pattern. The problem is that whenever I change the VM property which is binded to SelectedItem to null, the View doesn't "deselect" the currently selected item. This is my binding in xaml:
<DataGrid Grid.Column="0" Grid.Row="0"
ItemsSource="{Binding Path=Users}"
AutoGenerateColumns="False"
CanUserAddRows="False"
IsReadOnly="True"
SelectedItem="{Binding Path=SelectedUser, Mode=TwoWay}">
The SelectedItem binding works from the view to the VM thus in the SelectedUser property I always have the selected object. The problem is that in the VM I'm doing some stuff which sometimes changes the SelectedUser property to null so I would expect the datagrid to deselect the row as well. Instead, it remains selected and if I try to click on the same row, the property doesn't update. If I click on any other row, the property changes as expected.
Is there a way to make the datagrid deselect if it's binded property is set to null? Also I'm looking for a MVVM solution as I don't want to write code behind. I can solve this by writing code behind so don't waste time offering such solutions :)
l.e.: this is my property in the VM:
public RPLUser SelectedUser
{
get
{
return selectedUser;
}
set
{
selectedUser = value;
OnPropertyChanged("SelectedUser");
}
}
Thanks in advance!
Yeah may need to add the XAML UpdateSourceTrigger to update the UI.
I recommend to check the
Output Window
in visual studio and see if anyBinding
is failing.Are you sure when you select something, the selection updates into the
SelectedUser
property?Did u put a breakpoint in setter of
SelectedUser
and see that it is hitting when you select something on the datagrid?The reasons for this
Binding
to break could be many ...SelectedUser
is of different type than individualUsers
.SelectedUser
does not match by reference with any items inUsers
.The following code in my case works perfectly fine...
When I set
MyDGSampleWindow.MySelItem
as null, the datagrid propertly deselects. Perhaps you might need to give us more input on how are you actually setting the value as null.Did you try setting
IsSynchronizedWithCurrentItem="True"
in the xaml properties for the DataGrid? AFAIK, this will allow you to unselect it by setting the SelectedUser to null.I cannot test it at the moment, but you could also try to add this in the setter of your property:
(For
ICollectionView
to do anything, you will need to haveIsSynchronizedWithCurrentItem
set)Like I said, I cannot test this right now. Also, the setter of the property is probably not the best place to put it. Maybe create an event handler for the
PropertyChanged
event locally and put that logic there.Let me know if it helps, else I'll see if I can run a short test...
The DataGrid will not Deselect it automatically as
DataGridRow
'sIsSelected
property should be set toFalse
.You can do that by Setting a style on DataGrid.. like
The
IsSelected
property should be the of the object i.e in your caseRPLUser
should have a property IsselectedThen before you set the
SelectedUser
to null... just doSelectedUser.IsSelected=False
And dont forget to attach this style to the
DataGridRowStyle
in DatagridI am using WPFToolkit you can modify the style if you are targetting
.NET 4.0