Cannot set value of property on checkbox binding - wpf

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

Related

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.

How to update an object's inner property from UI to VM? (WPF MVVM)

I have a property in my ViewModel, I'll call it "Project" which contains several nested lists inside of it. None of such lists has an associated property in the view model since I can show everything in xaml by using triggers and bindings.
My xaml shows the Project hierarchy in a treeview and its details in several views (a content control selects the right view depending on which item is selected on the treeview). One of those "details" is a property for the objects contained in one of the nested lists, I'm showing it in a textbox so the user can edit it, the problem I'm having is I don't see that property updated in the Project property in the VM once I make changes to it in the textbox.
I was told I have to create a property in the VM for that specific object's property I'm trying to edit, I just don't know how since the object is deep inside one of the nested lists of my Project object.
Common mistakes are:
Using OneWay binding mode instead of TwoWay
In XAML:
<TextBox Text="{Binding Path=Name, Mode=TwoWay}" />
Not realizing that the update is not propagated to the VM until after the TextBox lost focus.
this is the default for the TextBox:
<TextBox Text="{Binding Path=Name, UpdateSourceTrigger=LostFocus}" />
You could change it to:
<TextBox Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}" />

set explicit source update while using data context

I need to update the binding source object with the control values only during a button click event.
But as my top level has datacontext being set, it's updating the source object whenever the control values change... Is it possible to set explicitly to update it during that event only keeping the datacontext as it is?
TextBox binding:
<TextBox x:Name="myTextBox" Text="{Binding MyTextProperty, UpdateSourceTrigger=Explicit}"/>
Button handler code:
myTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();

Xaml binding for radio/checkbox to toggle between imperial/metric

Pretty much as the title states, I'm grabbing some values from a Db, which are all in Km, but I want to implement a converter which I can toggle between Miles or Kilometers, and want to bind which is displayed to either a checkbox, or a radio button group, whichever is easiest (Radio would be preferred).
I'm thinking I can just use an IValueConverter rather than an IMultiValueConverter, and the Convert/ConvertBack methods, as the default will be Kilometers, but I don't know how to call the ConvertBack method. Or I could pass true/false as the ConverterParameter depending on whether I want Km/Miles displayed.
But either way I'm not sure how to hook up the Xaml Binding on either method (I know how to do a standard value converter binding, but not the extra flumff needed.
Any hints appreciated.
<StackPanel Grid.Row="0" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Right">
<RadioButton Content="Km" GroupName="rdBtnGrpValue" IsChecked="True" />
<RadioButton Content="Miles" GroupName="rdBtnGrpValue" />
</StackPanel>
And:
<TextBox HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Column="1" Text="{Binding EquatorialCircumference, Converter={StaticResource KmMiConv}, StringFormat='{}{0:0,0.0}'}" />
If you are using the MVVM pattern, and are using a view-model as your DataContext, you could use a Mode=TwoWay binding between the RadioButtons and a boolean property in the view-model, something like bool ConvertToImperial { get; set; }.
Your actual conversion can occur in the getter for the EquatorialCircumference property. If ConvertToImperial is true, return the value in miles, otherwise return the value in kilometres.
Then, for the TextBox, you can simply bind it to the EquatorialCircumference property, and the value displayed will be in the selected unit.
You will, however, need to raise a property change notification for any properties whose values are affected by a change in units.

WPF DataGrid Button Column Disable

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>

Resources