Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid Button Column Disable

Tags:

wpf

datagrid

Is it possible to disable a button in a DataGridTemplateColumn? I have a DataGridTemplate as follows:

<toolkit:DataGridTemplateColumn Header="Timer" Width="50">
                    <toolkit:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="Start" Click="Button_Click" CommandParameter="{Binding}" />
                        </DataTemplate>
                    </toolkit:DataGridTemplateColumn.CellTemplate>
                </toolkit:DataGridTemplateColumn>

The purpose of the Button is to start a timer recorded to the object associated with that row. My timer code works fine but I would also like to disable the buttons of every other row so that you can only have one timer running.

I used

WorkItemGrid.Columns[WorkItemGrid.Columns.Count - 1].GetCellContent(item).IsEnabled = false

to disable it and all the buttons correctly appear disabled but if you click on the button twice it will reenable and allow you to click on it a third time and trigger the Click event. Is it possible to actually disable the button?

like image 515
Stephan Avatar asked Dec 06 '25 17:12

Stephan


1 Answers

I would have the object the Datagrid is bound to expose a "IsEnabled" boolean property I can bind the button to. Whenever the handler is called, simply get the other object from your original collection and have them change their property to false. This will automatically disable the other buttons.

If your are not in control of the "timer" class, you can wrap it in your own class before databinding the grid to the collection of your objects.

like image 92
Denis Troller Avatar answered Dec 08 '25 09:12

Denis Troller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!