Reputation: 99
I want a button to be enabled only when a cell in a DataGrid is selected.
This line of XAML works when the DataGrid's SelectionUnit is set to "FullRow" :
IsEnabled="{Binding ElementName=dataGrid, Path=SelectedItems.Count}"
It no longer properly enables the button when the SelectionUnit of the DataGrid is set to "Cell"
Any idea why this is happening?
Are there any work good work-arounds?
Thanks
Upvotes: 0
Views: 201
Reputation: 99
It turns out that the SelectedCells.Count property of a DataGrid does not implement INotifyPropertyChanged.
My binding was not able to notify my data trigger when the number of selected cells changed, and the data trigger value was always 0 (even though the code-behind showed otherwise)...
I ended up binding my data trigger to an internal property that tracked the selected row index of the DataGrid, and checked for it to be equal to -1.
<Button.Style>
<Style TargetType="Button">
<Setter Property="IsEnabled" Value="True" />
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedRowIndex}" Value="-1">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
Upvotes: 1
Reputation: 37059
The DataGrid's SelectedCells.Count
should return a nonzero value when the DataGrid is selecting by Cells. A DataTrigger
in a Style on the Button
can bind to that and disable the button when Value="0"
.
Upvotes: 1