Why is CheckBox.IsChecked property Nullable<bool>? - c#

Why is the IsChecked property of a checkbox control in WPF of type bool? (or Nullable<bool>). I mean how can a checkbox control have the value of null?

Yes the null value exists and appears as a filled box. It indicates "Not Applicable" to the system.

According to the documentation, the IsChecked property has three different possible states:
So, when IsChecked is set to null, the check box will show an "indeterminate" state. This is commonly represented as a shaded, or greyed-out, control.

Checkboxes can have a 3rd, grayed, indeterminate state.

Don't forget to set the checkbox's property IsThreeState to true to enable this functionality.

Because WPF supports binding. If we bind a DB boolean column value to a checkbox. That column may have True/False/Null values. That means it has three values for a boolean field. So the WPF UI also should handle the three state.

Related

WinUI: does property "Disabled" exist for checkbox?

I read MSDN for WinUI but haven't found any information about how to disable checkbox. If it's exists, what's the name of property?
If it doesn't, how should I control, that checkbox cannot be changed? By overriding onClick?
Yes. Disabled doesn't exists, but all controls have Enabled property, and you can set it to False. This will disable your CheckBox, as you want.
You're probably looking for the IsEnabled property, available to all Controls.

Trigger the visibility of control based on count

This is just a generic question related to C# and WinForm..
Is there any way to automatically trigger visibility of a control based on the count..
For example, I have a Boolean count which could be true or false.. If the count is True I need to hide some control and if its false I need to show the control.
Is it possible that changing the value of Boolean control can trigger the visibility function automatically? So that there is no actual call of Show / Hide control.. When the count value changes it automatically triggers the function which checks for the count value and shows/hide the control?
At some point, there WILL be a call to Show() or Hide() or changing .Visible.
You can, in the designer, bind the control's .Visible property to the Count property (seriously reconsider the name for this) on your object. But that will really just pre-write the code for you.
When are you loading your object that has this boolean-count property? You could change the visiblilty of anything you like at that point.
Or, you could change the visiblity when your user edits the object you're presenting.

How to hide WPF AutoCompleteBox dropdown

I am using MVVM for my WPF application. I have a AutoCompleteBox in my xaml file which works normally. But now, I added a bool flag in my ViewModel isHideDropDown which will hide dropdown for the AutoCompleteBox if set to true. To summarise, I want my AutoCompleteBox to work as normal TextBox if isHideDropDown is set to true.
Can anyone give me some idea?
Thanks
Assuming you're using the AutoCompleteBox described here.
Setting the MinimumPrefixLength to -1 will disable the auto complete functionality of the control.
So, if you add an int MinimumPrefixLength property to your ViewModel that returns -1 when isHideDropDown is true, then you can bind to that. The other option would be to write a value converter that converts the boolean isHideDropDown value to the appropriate integer.
I have't tried this. But, instead of setting isHideDropDown to true, why not set the object that binds to ItemSource property as null?
In an autocompleteBox you need to set the ItemSource. When you don't want the autocomplete feature just set that object to null
You can also try to override ItemContainerStyle:
http://msdn.microsoft.com/en-us/library/dd795156%28v=vs.95%29.aspx#customizing_the_appearance_of_the_autocompletebox
Use DataTrigger with Binding to conditionally set ItemContainer's Visibility to Visible/Collapsed

Set value in UltraGridCell not firing events

I have an UltraGrid with checkboxes in one column. I have an event that is fired when one of the checkboxes is clicked (checked or unchecked).
However, I want to set the value of the checkbox through code at a later time. I figured out how to do this by finding the UltraGridCell and doing cell.value = true; or cell.value = false;, but this isn't firing the event, which I need. I also found cell.SetValue(true,something), but I am not sure what to pass into something. The docs are no help, and I can't find an example that does what I want. Any ideas?
Which event are you using to determine when the value of the cell changes? If you use UltraGrid.AfterCellUpdate, it will fire when the cell value is set programmatically, either with the Value property or the SetValue method. The "something" you're curious about in the 2 parameter overload is a boolean value which indicates whether the value change should go onto the undo stack. If you pass in True, the user can perform an undo on the grid and it will undo your programmatic change. If you just set the Value property, it does not get added to the undo stack.

ComboBox SelectedItem vs SelectedValue

The following code works as you’d expect — MyProperty on the model is updated when the user picks a new item in the dropdown.
comboBox1.DataBindings.Add("SelectedValue", myModel, "MyProperty", true,
DataSourceUpdateMode.OnPropertyChanged);
The following, however, doesn’t work the same way and the model update isn’t triggered until the input focus moves to another control on the form:
comboBox1.DataBindings.Add("SelectedItem", myModel, "MyProperty", true,
DataSourceUpdateMode.OnPropertyChanged);
Does anybody know why? I don’t even know where to start investigating the cause. Pointers in the right direction to start the investigation or an outright explanation would be equally appreciated.
Aside: for my purposes, I ended up binding to both SelectedItem and SelectedValue. This way I get instant model updates based on UI changes (through the SelectedValue binding), and UI updates based on programmatic model changes (through the SelectedItem binding).
The ComboBox control inherits from the ListControl control.
The SelectedItem property is a proper member of the ComboBox control. The event that is fired on change is ComboBox.SelectionChangeCommitted
ComboBox.SelectionChangeCommitted
Occurs when the selected item has changed and that change is displayed in the ComboBox.
The SelectedValue property is inherited from the ListControl control.
As such, this property will fire the ListControl.SelectedValueChanged event.
ListControl.SelectedValueChanged
Occurs when the SelectedValue property changes.
That said, they won't fire the INotifyPropertyChanged.PropertyChanged event the same, but they will anyway. The only difference is in the firing event. SelectedValueChanged is fired as soon as a new selection is made from the list part of the ComboBox, and SelectedItemChanged is fired when the item is displayed in the TextBox portion of the ComboBox.
In short, they both represent something in the list part of the ComboBox. So, when binding either property, the result is the same, since the PropertyChanged event is fired in either case. And since they both represent an element from the list, the they are probably treated the same.
Does this help?
EDIT #1
Assuming that the list part of the ComboBox represents a property (as I can't confirm since I didn't write the control), binding either of SelectedItem or SelectedValue affects the same collection inside the control. Then, when this property is changed, the same occurs in the end. The INotifyPropertryPropertyChanged.PropertyChanged event is fired on the same property.
I suspect that the SelectedItem property of the ComboBox does not change until the control has been validated (which occurs when the control loses focus), whereas the SelectedValue property changes whenever the user selects an item.
Here is a reference to the focus events that occur on controls:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validated.aspx
This is a long-standing "feature" of the list controls in .NET in my experience. Personally, I would just bind to the on change of the SelectedValue property and write whatever additional code is necessary to workaround this "feature" (such as having two properties, binding to one for SelectedValue, and then, on the set of that property, updating the value from SelectedItem in your custom code).
Anyway, I hope that helps =D
If you want Selected.Value being worked
you have to do following things:
1. Set DisplayMember
2. Set ValueMember
3. Set DataSource (not use Items.Add, Items.AddRange, DataBinding etc.)
The key point is Set DataSource!
If we want to bind to a dictionary ie
<ComboBox SelectedValue="{Binding Pathology, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{x:Static RnxGlobal:CLocalizedEnums.PathologiesValues}" DisplayMemberPath="Value" SelectedValuePath="Key"
Margin="{StaticResource SmallMarginLeftBottom}"/>
then SelectedItem will not work whilist SelectedValue will

Categories

Resources