Make checkbox bound to nullable bool transition from null to true - wpf

I have a checkbox with its IsChecked property bound to a nullable bool. When my control first loads the value is null and the checkbox appears greyed out. This is what I want.
When the user clicks the checkbox, it moves to the false/Unchecked state.
However, 99% of the time the user is going to want to tick the checkbox - which currently means double clicking the checkbox.
How can I make the value move from null to true when the user first clicks the checkbox?

I had the same problem and ran into this question. This is a late response but I think this is the best solution :)
With the help of IsThreeState and TargetNullValue you can accomplish this
<CheckBox IsThreeState="False" IsChecked="{Binding YOUR_NULLABLE_PROPERTY, TargetNullValue=false}" />
The only caveat is that it will toggle between null and true. There will never be a false value.

You can just modify the setter of the bound property to check whether the previous value is null and if it is, set the value to true. Something like this:
public bool? MyBoolProperty
{
get { return _myBoolProperty; }
set
{
_myBoolProperty = (_myBoolProperty != null || value == null) ? value : true;
RaisePropertyChanged("MyBoolProperty");
}
}
The binding system will re-read the property after it sets it, so the new value will be reflected by the CheckBox.

You can handle the Click event and implement a logic like this:
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
switch (cb.IsChecked)
{
case null:
cb.IsChecked = false;
break;
case true:
cb.IsChecked = true;
break;
case false:
if (cb.IsThreeState) {
cb.IsChecked = null;
} else {
cb.IsChecked = true;
}
break;
}
e.Handled = true;
}

I have met this problem recently. I look all answers of this question, but none seem fit me. The accepted answer is fail when I set IsChecked equals false in ViewModel. I decompile the checkbox in wpf.
As you can see, when you click a null IsChecked checkbox, IsChecked property will switch to false.
protected internal virtual void OnToggle()
{
bool? flag;
if (this.IsChecked == true)
{
flag = (this.IsThreeState ? null : new bool?(false));
}
else
{
flag = new bool?(this.IsChecked.HasValue);
}
base.SetCurrentValueInternal(ToggleButton.IsCheckedProperty, flag);
}
So you can Create a new class inherit CheckBox and override OnToggle method like this:
protected override void OnToggle()
{
bool? flag;
if (this.IsChecked == true)
{
flag = this.IsThreeState ? null : new bool?(false);
}
else
flag = true;
// what a pity this method is internal
// actually you can call this method by reflection
base.SetCurrentValueInternal(ToggleButton.IsCheckedProperty, flag);
}
The other way is:
protected override void OnToggle()
{
if (this.IsChecked == null)
this.IsChecked = false;
base.OnToggle();
}

The easiest way would be to simply handle the click event and set the control to true if its current state is null, optionally setting a flag for your internal code tracking after first click.

Related

How to uncheck checkbox in datagridview using Entity framework

I have datagridview on winform. It is bound to result from below code:
PoolEntities db = new PoolEntities();
var Result = db.General_Pool_Detail.Where(g => g.Pool_Name == cbxGLType.SelectedValue && g.Mapped_Date == dt).Select(s=>
new { Selected = true, s.Gen_Pool_ID, s.GSL_Code, s.Amount }).ToList();
dgvGeneralPoolData.DataSource = Result;
The code works perfectly fine. But when I uncheck the checkbox on datagridview it does not work.
In datagirdview event i have written the following code:
private void dgvGeneralPoolData_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dgvGeneralPoolData.Rows[e.RowIndex].Cells[0].Selected)
{
Boolean IsChecked = (Boolean) dgvGeneralPoolData.Rows[e.RowIndex].Cells[0].Value;
if (IsChecked)
{
dgvGeneralPoolData.Rows[e.RowIndex].Cells[0].Value = false;
}
}
}
Also, I want to save the changes made in datagridview to the database.
Please help.
EditMode Property
check editmode property of your datagridview.
Does it even hit your click event? Where exactly does it break when you debug? Try to debug and provide some more info if you can.
1) Debug and make sure that the event handler does work. Sometimes when you copy paste the code the event handler doesn't get register by the designer so try deleting the CellClick and type the code again on the grid view, when you add the = sign it should give you an option to create the ClickCell method for you. It should be something like this inside your click event.
if (e.RowIndex != -1)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dgvGeneralPoolData.CurrentRow.Cells["ColumnNumberHere"];
if (chk.Value == null || chk.Value = false)
{
chk.Value = true;
}
else
{
chk.Value = false
}
}
2) Make sure this is under your InitializeComponent()
this.dgvGeneralPoolData.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvGeneralPoolData_CellContentClick);

Threestate checkbox inital binding Windows Store App / RT

my checkbox doesn't bind the initial value correctly on startup:
<CheckBox IsThreeState="True" IsChecked="{Binding StartWithSettings,Mode=TwoWay}"/>
The checkbox is displayed empty on startup, but should be nulled (the box with the black square in it).
Getter is properly raised with null on startup, what am I missing?
EDIT:
Below you can find my viewmodel, model code and a button to switch all three states of the checkbox. the strange thing happens when switching between null state and false state -> it always shows the false checkbox, whether its null or false, but the underlying data is correct.
do i toggle the checkboxstate directly by clicking the checkbox itself, all three states are displayed correctly.
It is a windows 8.1 store app, maybe the wpf checkbox control is another than the "usual" wpf checkbox and has a bug?
viewmodel snippet:
public bool? StartWithSettings
{
get
{
return _configurationModel.MyAppModel.StartWithSettings;
}
set
{
_configurationModel.MyAppModel.StartWithSettings = value;
RaisePropertyChangedEvent("StartWithSettings");
}
}
model snippet
public class MyAppModel
{
public bool? StartWithSettings { get; set; }
public MyAppModel()
{
this.StartWithSettings = null;
}
}
snippet test code
private void ChangeCheckboxState()
{
if (StartWithSettings == null)
{
StartWithSettings = true;
return;
}
else if (StartWithSettings == true)
{
StartWithSettings = false;
return;
}
else
StartWithSettings = null;
}
The backing property that is bound to the property must be of type bool? in order to support 3 state check box. Make sure that the backing field is set to null.
Also, you don't need to set IsThreeState="True"
EDIT: Since you mentioned Windows 8.1 and I assume this would be WinRT.
Nullable types are not supported in WinRT/Win8 Dev.
A solution would be here

Cancelling selection in combobox in wpf using MVVM

I have a combobox of type List. I have the ItemsSource and the ItemSelected bound through the datacontext. If the selected item has been changed then I show a pop up message confirming the users action. On clicking of 'Ok' the selection gets changed. But on clicking of cancel, the selection should be cancelled and previous item should be retained. Below is the property that is bound to SelectedItem of the combobox.
Public SomeClass Sel
{
get
{
return _sel;
}
set
{
if (_sel != value)
{
var sview = _sel;
if (Compare())
{
_sel = value;
if (Sel != null)
IsDefault = Sel.IsDefault;
OnPropertyChanged(() => Sel);
}
else
{
MessageBoxResult result = MessageBox.Show("Message.", "Owb Message", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
_sel = value;
if (Sel != null)
IsDefault = Sel.IsDefault;
OnPropertyChanged(() => Sel);
}
else
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
_sel = sview;
OnPropertyChanged("Sel");
}), DispatcherPriority.Send, null);
return;
}
}
}
}
}
The combo box is in a pop window. So would Dispatcher object work in that case?
I'm guessing the selected value is retained, but the View doesn't update correctly.
Have a look at this article: http://www.codeproject.com/Articles/407550/The-Perils-of-Canceling-WPF-ComboBox-Selection. Basically, the few workarounds that did exist in .Net 3.5 no longer work in .Net 4.0..
As a general rule, if you've got visual controls leaking into your viewmodel, you're going down a path you don't want to go down.
Create a Behavior that intercepts the OnChanged event of the ComboBox and launches a message box. Here's a tutorial on using behaviours
This keeps all the UI logic in the UI and leaves your viewmodel to manage data and validation.
It works like magic now! I missed out setting the value before calling dispatcher.
_sel = sview

TextBox resets text when tabbing out ot other controls

I have a form with several controls. There a situtions where 'textBoxOtherRelationship' is disable and the text is set to string.empty. But when I then got to another control and tab out the data appears again,while the control remains disabled.
textBoxOtherRelationship.DataBindings.Add(new Binding("Text", _binder, "RelationshipNotes"));
private void ComboBoxRelationShipSelectedValueChanged(object sender, EventArgs e)
{
if ((Constants.Relationship)comboBoxRelationShip.SelectedItem.DataValue == Constants.Relationship.Other)
{
textBoxOtherRelationship.Enabled = true;
if (_formMode != ActionMode.ReadOnly)
{
textBoxFirstName.BackColor = Color.White;
}
}
else
{
textBoxOtherRelationship.Enabled = false;
_model.RelationshipNotes = null;
textBoxOtherRelationship.Text = string.Empty;
if (_formMode != ActionMode.ReadOnly)
{
textBoxFirstName.BackColor = Color.LightYellow;
}
}
}
Hmm.. so I see this line here:
textBoxOtherRelationship.DataBindings.Add(
new Binding("Text", _binder, "RelationshipNotes"));
which tells me that you've got binding set up between the Text property on the textBoxOtherRelationship and a property called "RelationshipNotes" on the datasource _binder.
Great.
So, I'm assuming that the two-way binding works just fine and that when you type something into the textBoxOtherRelationship and that control loses focus the underlying RelationshipNotes property is getting updated as well, right?
Now, looking at your code there, I don't think the underlying datasource is being updated when you set the Text property to string.Empty because that usually doesn't happen until the textbox loses focus and you've disabled the control.
If you add:
textBoxOtherRelationship.DataBindings[0].WriteValue();
after you set the value to string.Empty that string.Empty value will get stored back to the datasource because the databinding will know there is something to update. Programmatically, it doesn't.
I see you have this line:
textBoxOtherRelationship.Enabled = false;
_model.RelationshipNotes = null; <<<----------------------
textBoxOtherRelationship.Text = string.Empty;
Is _model.RelationshipNotes what is ultimately supposed to be bound to that textbox?
The SelectedIndexChanged event doesn't commit the databindings until after the control loses focus, so the quick fix is to write the value first in your event:
private void ComboBoxRelationShipSelectedValueChanged(object sender, EventArgs e)
{
if (comboBoxRelationShip.DataBindings.Count > 0) {
comboBoxRelationShip.DataBindings[0].WriteValue();
if ((Constants.Relationship)comboBoxRelationShip.SelectedItem.DataValue ==
Constants.Relationship.Other) {
textBoxOtherRelationship.Enabled = true;
if (_formMode != ActionMode.ReadOnly) {
textBoxFirstName.BackColor = Color.White;
}
} else {
textBoxOtherRelationship.Enabled = false;
_model.RelationshipNotes = null;
textBoxOtherRelationship.Text = string.Empty;
if (_formMode != ActionMode.ReadOnly) {
textBoxFirstName.BackColor = Color.LightYellow;
}
}
}
}

Is there any way of checking if a DataGrid in Silverlight has Focus?

I have a Silverlight DataGrid of which I need to check if it has Focus. I know there is a method to set Focus and an event for GotFocus but can't see anyhting for checking if it has focus.
Any Ideas ?
AFAIK there is no direct method or property to check if it has focus, but you should be able to use the FocusManager.GetFocusedElement().
If you then define a extension method, you should be able to call MyDataGrid.HasFocus():
public static class ControlExtensions
{
public static bool HasFocus(this Control aControl)
{
return System.Windows.Input.FocusManager.GetFocusedElement() == aControl;
}
}
[edited: I did test it now:]
However there is catch: the call GetFocusedElement() can return the current focused cell within the DataGrid. So in that case the HasFocus will return false.
To be able to check if the DataGrid or one of its cells are focused, we can adapt our extension method like this
public static class ControlExtensions
{
public static bool HasFocus(this Control aControl, bool aCheckChildren)
{
var oFocused = System.Windows.Input.FocusManager.GetFocusedElement() as DependencyObject;
if (!aCheckChildren)
return oFocused == aControl;
while (oFocused != null)
{
if (oFocused == aControl)
return true;
oFocused = System.Windows.Media.VisualTreeHelper.GetParent(oFocused);
}
return false;
}
}
Hope this helps a bit?

Resources