WPF DataGrid Button Column Disable - wpf

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?

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.

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).

its not working in my case.
<DataGridTemplateColumn Width="70" Header="Refund" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="btnRefundGame" IsEnabled="{Binding RESUND_STATUS}" CommandParameter="{Binding Path=IDEN_LOGID}" Content="Refund" Click="btnRefundGame_Click" ></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Related

Cannot set value of property on checkbox binding

<CheckBox x:Uid="cbRFEnableManualControl"
Grid.Column="0"
Grid.Row="0"
Name="cbRFEnableManualControl"
VerticalAlignment="Center"
Margin="0,0,0,12"
IsChecked="{Binding RFEnableManualControl, Mode=TwoWay}"
Checked="RFEnableManualControlChanged"
Unchecked="RFEnableManualControlChanged">
<CheckBox.Content x:Uid="EnableManualControlContent">
Enable manual control
</CheckBox.Content>
</CheckBox>
If I check this checkbox then i should enable some controls. I have a property "RFEnableManualControl" in my data model class, based on this value am enabling/disabling the other controls.
Issue is, i have another one button, if i check this checkbox after clicking this button the value is not set for this property. the execution is not going into this property and directly hitting the RFEnableManualControlChanged event.
In this another one button am calling the constructor of this data model class but am not assiging anything to this particular property.

Set WPF CheckBox in a ComboBox to Checked

I have a custom WPF control - essentially a combo-box with check-boxes. The combo-boxes are successfully bound to a list of available items.
This control is to represent a field on a form. It is possible for the form to be completed already, in which case, the control must be rendered with the selected items - i.e. any items previously selected must be rendered as a checked CheckBox; it is here I'm running into trouble.
My first thought was to simply bind the IsChecked property - I don't think this can be done as the list of currently-selected-items is different from the list of available items which the ComboBox is bound to.
Basically, how do I gain access to the CheckBox object to set the Checked property to true? I've looked into this extensively and I can't fathom this out.
I'm using the ItemContainerGenerator approach - so when the user clicks on the drop-down, it is intended that a handler will iterate through the CheckBoxes and set the relevant boxes to checked.
Here's the XAML:
<ComboBox x:Name="FieldOptions"
ItemsSource="{Binding}"
HorizontalAlignment="Stretch"
Height="30"
KeyDown="FieldOptions_OnKeyDown">
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox Name="checkbox"
Content="{Binding Path=Text}"
Uid="{Binding Path=ID}"
FontStyle="Normal"
Foreground="Black"
Checked="CheckBox_OnChecked" />
</DataTemplate>
</ComboBox.ItemTemplate>
Any thoughts would be much appreciated.

Have two controls set the visibility of another control

Sorry for the title, I just don't know how to explain it in one sentence.
So here is my goal: I need to have a boolean in my ViewModel define the visibility for a control (border).
I know I can achieve this with a BooleanToVisibilityConverter, but there is a little more to it. I want a button on my UI to be shown if the control is not visible. Once that button is pushed, then I want the boolean in my ViewModel to be TRUE and then I want the control to be visible and the button that was just pushed to be collapsed. Once that control is visible, I would like a button within that recently visible control to make the control collapsed and then make the original button visible.
Basically, there are two buttons: 1 to make visible (then collapse itself) and the other is to collapse its container and then make the first button visible.
I am trying to do all this with MVVM so if I can avoid code behind in my View that would be ideal!
Since you're using ICommands on your viewmodel, this should work...Assume your commands are "ShowBorderCommand" and "HideBorderCommand" and the property on your viewmodel is "ShowBorder"
<ConverterNamespace:BooleanToVisibilityConverter x:Key="BoolToVis"/>
<ConverterNamespace:ReverseBooleanToVisibilityConverter x:Key="BoolToCollapse"/>
<Border Visibility="{Binding ShowBorder, Converter={StaticResource BoolToVis}}">
<Button Command="{Binding HideBorderCommand}"/>
</Border>
<Button Command="{Binding ShowBorderCommand}" Visbility="{Binding ShowBorder, Converter={StaticResource BoolToCollapse}}"/>
My WPF Converters library has a BooleanToVisibilityConverter that allows reverse conversions, as well as allowing the use of Hidden instead of Collapsed:
<con:BooleanToVisibilityConverter x:Key="ReverseBooleanToVisibilityConverter" IsReversed="True"/>
<Button Visibility="{Binding SomeProperty, Converter={StaticResource ReverseBooleanToVisibilityConverter}}"/>

Prevent DataGrid row from being deleted

I want to protect some rows of a DataGrid to be protected from deletion by the user, although the property CanUserDeleteRows is set to true.
Is there a possibility to protect some rows, hopefully through a databinding or a trigger? The ItemsSource is bound to an ObservableCollection of T.
If you have a property on your bound objects that can be used to determine if the current row can be deleted , like 'IsDeleteEnabled', then you can bind the DataGrid's CanUserDeleteRows property to SelectedItem.IsDeleteEnabled.
For example,
<DataGrid Name="dataGrid1"
CanUserDeleteRows="{Binding ElementName=dataGrid1, Path=SelectedItem.IsDeleteEnabled}"
Never done this with a DataGrid. Usually, when I need to control something like this, I use a ListBox and a DataTemplate with a Grid within to give it the idea of a Grid or a ListView with a GridView in the template because they both give you more control over the interaction.
A shot in the dark, since you're binding, you could use a DataGridTemplateColumn.CellEditingTemplate and make your own Delete button/text which is visible or enabled based off logic within your binding object. Maybe something like this (I didn't test this, but it should be a direction you can head)?
<dg:DataGridTemplateColumn Header="Action">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Text Content="Delete" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
<dg:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ButtonEnabled="{Binding Path=IsDeleteEnabled, Mode=OneWay}" Content="Delete" Command="{Binding Path=DeleteMe}" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellEditingTemplate>
</dg:DataGridTemplateColumn>
Using this method, since the command is bound to the individual object, you would probably have to raise an event your screen's ViewModel handles to remove that row from the ObservableCollection.
Again, not sure if this is the best way, but it's my 10 minute stab at it. So if it's horrible, please don't vote me down too much.

How to trigger PreparingCellForEdit event from a (Edit)button click in Silver light 4

I have a Data Grid in Silverlight 4 with 3 columns along with a column which contains "Edit/Apply" button.
The row cells are initially rendered as plain text and I need them to be changed to Comboboxes in the edit mode.
Once the Edit button in any of the row is clicked. I need to change the textblock( This is my Cell Template) in one of the row to the ComboBox(This is my Cell Editing template)
The question is how do i facilitate this on clicking the Edit button of each row and not by double clicking on the row.
Thanks,
Vijay
1st way
Put the textblocks on top of the combo-boxes (comboboxes with collapsed visibility). On Edit Switch visibilities between controls (Combo - visible / TextBlock - Collapsed) and Bind the Text Property from the Textblock to the selected value from the combo.
2nd way
Put only combo-boxes with IsReadOnly Property set to True. On Edit set IsReadOnly to false and on save set it back to true.*
3rd way
Make the datagrid readonly and bind a Data Form to it. The Data Form contains edit / save / cancel buttons.
If you need an example just let me know and I'll write one as soon as possible.
Not sure if this is what you expected. If not, please just ignore it. It is possible that I missunderstood the question.
Another answer
The other answer will be to use a DelegateCommand binded on the Command property of the Edit button wich can contain a parameter (the row number). This is if you are using the MVVM pattern. And in the ViewModel you could edit the selected row.
After a bit of searching / trying i was able to toggle between display and edit mode by a button click (button placed in each row).
Below posted is the sample code , which facilitates this toggle for one of the Cells in the Grid, Which makes use of Two Boolean Properties ShowDefaultTemplate and ShowEditableTemplate , The VisibilityConverter converts the boolean values to corresponding Visibility Options (Visible or Collapsed).
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding XXX}" HorizontalAlignment="Center" VerticalAlignment="Center"
Visibility="{Binding ShowDefaultTemplate, Converter={StaticResource visibilityConverter}}" />
<ComboBox HorizontalAlignment="Left" MinHeight="24" Width="100"
ItemsSource="{Binding Source, Source={StaticResource Provider}}"
Visibility="{Binding ShowEditableTemplate , Converter={StaticResource visibilityConverter}}"
SelectedItem = "{Binding SelctedItem,Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
Thanks,
Vijay

Resources