tags:

views:

2090

answers:

2

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?

A: 

If it is acceptable to disable the button that was clicked in addition to the others, then I would bind Button.IsEnabled to a property that is set to false once the timer is started, and then change it back to true once the operation has finished.

If that is not acceptable, then I'm not sure if there is a way to do this, since by definition the template is used to create the controls in each row. Well, you could search the visual tree for all the other buttons, but that just doesn't seem like a good thing to do (not to mention it could be slow for a large amount of data).

Andy
I though that too, but I need to be able to leave the button that was clicked enabled. The button that was clicked needs to be changed to say something different and the rest of the buttons need to be disabled.
Stephan
+1  A: 

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.

Denis Troller