SelectionChanged event not firing for derived ComboBox control - WPF - wpf

I have a derived Combobox control. In Autogeneratingcolumns event, I have assigned FrameworkElemnt of derived combobox as shown below:
var templateColumn = new DataGridTemplateColumn
{
CellTemplate = new DataTemplate
{
VisualTree = derivedComboFrameWorkElement
}
};
grid.Column = CreateTemplateColumn(templateColumn);
But, the SelectionChanged event does not fires for the combobox. The funny thing is that, once I inspect the visual tree (GridCell and my ComoboBox) using WPF-Inspector, the SelectionChanged event fires. So doubting that some issue with VisualTree updates. Please help me to get this working properly.
Regards,
ani

Good news. Issue has been identified. And the answer is, there was a PreviewMouseDown event and Focus() was called in the event. And thereby the dropdown was closed and was not available for click. I corrected the logic to solve the issue.
To identify this kind of issues, we can make use of WPF Inspector to check visual tree and Snoop which shows all events invoked. Snoop helped me to analyse the issue. Thanks for help.
Regards, ani

Related

Using triggers to fire the Items.CurrentChanging event on a tab control

I am using MVVM for a project and my question relates to using triggers.
The Items property of the TabControl has a property called items and items has an event called CurrentChanging. (TabControl.Items.CurrentChanging)
How do I wire up an event on the child of my main object using triggers?
Thanks
Edit:
This is using WPF and the MVVM-Light toolkit
This was solved by creating a Routed Event that was raised when the CurrentChangingEvent was fired in the code behind.
To make it fire I needed to add in the IsSynchronizedWithCurrentItem = true;

Resetting SelectedItem on a Listbox bound to a RelayCommand on a ViewModel

Using the EventToCommand behaviour that comes with MVVM Light I am binding the SelectedItem of a ListBox to a RelayCommand on a ViewModel
All works great in my Windows Phone 7 app except that after navigating away from the View with the ListBox then back the SelectedItem is the same as before. Not what I want.
I tried reseting the selected index when navigating away but that causes the Command to trigger again.
Has anyone else solved this issue and how?
TIA
Patrick Long
I handle this by a simple check at the start of each handler. Assuming that you are only supporting the single selection of items you can check if there is an added item.
if (e.AddedItems.Count == 1)
{
// Your code here
}
(Where e is an instance of SelectionChangedEventArgs.)
If the selection has been removed the AddedItems list will be empty but the RemovedItems list will be populated instead.
Since you're using mvvm light can't you call the clear method in the ViewModelLocator for the specific view model?
I switched away from using the SelectionChanged event. Now I do it all with TapGestures. Downside of this is that TapGesture does not work with EventToCommand behaviour so I am trapping the Gesture event in the View and firing the Command that is bound to the DataContext of the Sender.
IMHO this is a lot neater than using SelctionChanged and mucking about with SelectedIndexes.

WPF ComboBox DataBound Event?

I use ComboBox.ItemsSource=[some data collection] to bind data to the control.
I want to hookup an event handler to the combobox so that whenever its data updated (or first time bound), I can do somthing.
The problem is I can't find an appropriate event for it. The close guess is DataContextChanged. But that is not invoked when the items get bound/created.
Many thanks in advance for any helps.
Cheers~
The ComboBox.Items property is of type ItemCollection, which has CollectionChanged, CurrentChanged, CurrentChanging events. They should suit your needs.
ItemCollection Class MSDN Article

WPF: Is there a "BeforeSelectionChanged" event for the combobox?

Is there a "BeforeSelectionChanged" event for the combobox? I want to verify some stuff before the SelectedItem property changes.
There's no PreviewSelectionChanged event. Instead of using two way binding, use one way binding to SelectedItem and get updates through command or SelectionChanged event. That way you can in the handler do some verification and even fake a cancel of the selection.
I don't think that there is, unfortunately.
You might be able to use the PreviewLeftMouseDown event and determine if the mouse is over an item in the ComboBox. If it is over an item that isn't the SelectedItem, you know it is about to change.

Silverlight Datagrid Refresh Data with SelectionChanged Binding

I am building an issue tracking system that uses Silverlight. I use DataGrids to display the issue lists, set the selected index to -1 so that no row appears selected and then use the selection change event to popup an issue details window for the particular selected issue.
When I try to refresh the DataGrid by rebinding it to its ItemsSource, I disable the SelectionChanged event, rebind the DataGrid to its ItemsSource, set the SelectedIndex to -1 and then enable the SelectionChanged event again. However, no matter how late I leave the re-enabling of the SelectionChanged event (even until after the DataGrid_Loaded event), a SelectionChanged event is fired and the issue details window pops up.
Is there a better way to refresh the data in a DataGrid that won't cause the SelectedIndex to change? If not, is there a way of telling which events are caused by a programmatic index change and not a human interaction?
(Also up for discussion, is this the best control for the job? I need to display multiple fields per row, such as the issue title, assigned user, requested by user, status, etc.)
Thanks in advance.
I have had a similar issue in the past with the comctl32 ListView control's selection events: Programmatic selection cause selection change events to be raised.
My workaround for this issue is to have a per-grid/list counter variable that lets the event handler know if it should care about the selection event or not. The code would go something like:
int issueList_ProgrammaticEventCount_Selection = 0;
void refreshIssueList()
{
++issueList_ProgrammaticEventCount_Selection;
issueList.ItemsSource = ...;
}
void issueList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (issueList_ProgrammaticEventCount_Selection > 0)
{
--issueList_ProgrammaticEventCount_Selection;
return;
}
showIssueDetails();
}

Resources