Reputation: 2030
I am new to WPF. I have one project which have one tab control consists of 2 tabs. To default selection the first tab item have the property 'IsSelected=True'.
This working fine. But i need to capture the user changed events which means if the user changed the selection, i need to capture that change with value. For this i created one event arguments by implementing 'FrameworkElementHandlerEventArgs'
My existing code like below;
public class SelectionChanagedEventArgs : FrameworkElementHandlerEventArgs
{
public object SelectedItem { get; set; }
}
I am registering this event to capture the changes
RegisterEvent<SelectionChangedEventArgs>(
(sender, e) =>
{
if (e == null)
{
return;
}
//Here i got the selection changes
},
handler,handler);
This is my code part only.
This is working fine. The issue is the default selection. When the application launches, it fires the default selection and got the changes in the event. But i need to capture only the user changed event. Any option to check or detect the changes done by user only
Upvotes: 1
Views: 1134
Reputation: 169200
You could set the IsSelected
property of the first TabItem
programmatically in your code before you hook up the event handler.
Or you could use a variable that keeps track of the number of times your event handler has been invoked and then simply return immediately the first time:
int count;
RegisterEvent<SelectionChangedEventArgs>(
(sender, e) =>
{
if (e == null || count++ == 0)
{
return;
}
//Here i got the selection changes
}, handler, handler);
Or check if the view has been loaded:
if (e == null || !IsLoaded)
There is no specific "user changed event". It is the exact same event that gets fired regardless of whether the user actually made an active selection or not.
Upvotes: 1
Reputation: 2030
I added IsLoaded property to verify whether the control is loaded for presentation. Now working fine. Please share if you think any another issue impact due to this.
The following is my update code:
RegisterEvent<SelectionChangedEventArgs>(
(sender, e) =>
{
if (e == null || !(sender as System.Windows.Controls.Control).IsLoaded)
{
return;
}
//Here i got the selection changes
},
handler,handler);
Upvotes: 0
Reputation: 2839
You might try to check if there was a previous selection:
tab.SelectionChanged += (sender, e) => {
if (e.RemovedItems.Count == 0)
return;
//Here i got the selection changes
};
where tab
is your TabControl
. You don't need to create your custom class with event arguments.
Upvotes: 0