How to prevent CheckBox getting clicked while ComboBox is closed? - xaml

I have a ComboBox which has a CheckBox control as a DataTemplate, and with that when the user tries to open ComboBox they sometimes accidentally end up selecting/deselecting the ComboBox, So what I want to do is to disable that behavior and open the ComboBox menu instead.
My ComboBox looks something as below:
Is there any way to achieve that? I tried modifying ItemTemplate of the ComboBox but so far didn't find any solution for it.
For now what I can do is just to bind CheckBox IsEnabled property with ComboBox IsDropDownOpen property, but if there's any better solution which you can suggest would be helpful.

Instead of toggling IsEnabled you should toggle IsHitTestVisible property of your checkbox. So when it is set to false it will not receive any mouse click events, but it will still look enabled because it still can receive keyboard events. So if you use tab navigation to tap it then it would work.

Related

ActiveControl is hidden

To boil the situation down, I have a panel with 3 textboxes on it. When I hide the panel, the first textbox in the tab order for the panel gets assigned to Me.ActiveControl (The form), regardless of which one was focused when the panel was hidden.
I thought a hidden control could never have focus. I have other controls on my form where the issue was originally found, so I thought it would have to choose a non hidden control to focus on. Is this a bug, or designed this way?
I use a timer on my simple form that fires every 5 seconds, recording the active control name in a label.
I have been able to recreate your scenario and it revealed an interesting possibility (more on this later).
WinForms has the concept of the Selected (or activated) control. The Control.Select Method is related to the Control.Focus Method. The Focus Method-Remarks section documentation is relevant.
A control can be selected and receive input focus if all the following
are true: the Selectable value of ControlStyles is set to true, it is
contained in another control, and all its parent controls are both
visible and enabled.
...
Focus is a low-level method intended primarily for custom control
authors. Instead, application programmers should use the Select method
or the ActiveControl property for child controls, or the Activate
method for forms.
The ContainerControl.ActiveControl Property points to the last selected control.
The reason that the first control by tab order in the Panel is selected is due to the code that executes when the Panel.Visible property is set to false. The Visible Property setter calls SetVisibleCore that in turn calls SelectNextIfFocused that calls SelectNextControlInternal that finally calls Control.SelectNextControl that selects the your TextBox1.
This is where it gets interesting. At this point the Panel and TextBox are both visible. Therefore, the TextBox receives focus and retains it when the Panel is hidden. This condition allows for a hidden TextBox to have keyboard input and no rules about a hidden control not being able to receive focus are violated.

Silverlight Usercontrol in Datagrid does not fire the CellEditEnded event

I have a UserControl that is displaying in a DataGrid. It contains a DatePicker and a Button that opens a Popup control where the user makes a selection that once selected populates a textblock with the result. The DatePicker is the only object that if changed will set off the CellEditEnded event. How can I get the popup controls selectionChanged event to tell the Datagrid that the cell has done as edit?
Turns out I was putting the UserControl inside the CellTemplate when I needed to put something else in that for display and my UC in the CellEditingTemplate. Now it fires as it should. I missed something simple and I should not have. Hope this helps someone else.

Windows store xaml add button with click event handler to gridviewitem

I have a gridView the data is coming from a datasource.
How can i add a button to a gridviewitem datatemplate and add a click event handler to the button?
An alternative way is to not actually use a Button at all and simply setting IsItemClickEnabled to true and using the ItemClick event.
If you genuinely need to have a button in your ItemTemplate - the easiest way to add an event handler is to create a UserControl for use in the ItemTemplate, put the button in it and add the event handler inside of that UserControl.
The appropriate way to do this is to bind the command of your button to a command property on the DataContext of the item which is templated.

How do you keep a listview row highlighted after its activated?

I have an application I'm working on that uses listviews. When you select a row from the list view it becomes highlighted, however when I double click on it to activate the listview item it is no longer highlighted. How would I get the listview item to stay highlighted upon activation, is this possible? Thanks.
It sounds like you are somehow losing focus from the control. If that logic is correct, then you can set the listview's .HideSelection = False property to retain the visual selection when the control loses focus.

how to disable the default selection in listview when you navigate to it

I have two list views in a scene (listview1 and listview2, contents of listview2 depends on the selecteditem on listview1) and when I navigate to the scene I want the first item in listview1 to be selected and the details be displayed in the other listview (listview2) but I do not want any item in the listView2 selected by default.
What i am observing is that the first item in listView2 is also selected and that is causing selectionchanged event to be triggered for listview2 (which I want to be triggered only if the user explicitly selects it)
No need for workarounds.
Just set IsSynchronizedWithCurrentItem="False" in your ListView.
This also works for GridViews.
selectionchanged events will be triggered and cannot be stopped. But it can be bypassed (using a flag) in the code behind.
If ListView is bound to CollectionViewSource, CollectionViewSource sets the first items as selected.
If ListView is bound to regular Collection, no items will be selected automatically.
This is because you set the event handler of list view 1 & 2 by XAML, maybe.
If you set the data source and selected item first, then,
set the event handler by your code behind, it works well, I think.