Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ComboBox.Selected attached event. exist or not?

Visual Studio (2015) XAML editor provide in the auto-complete ComboBox member list, an event Named Selected. What is the explanation for this auto-complete mistaken? auto complete

Selected its NOT WPF ComboBox event (event list) but its an event of ComboBoxItem (inherited from ListBoxItem.Selected).

why is it?

EDIT

as @glenThomas'answer (thank), the Selected its attached event inhrited from Selector, for easy listen for all child selected event.

But, its not work... if i put an handler in Selected and i build the project i receive an error:

'ComboBox' does not contain a definition for 'Selected' and no extension method 'Selected' accepting a first argument of type 'ComboBox' could be found

like image 486
dovid Avatar asked Sep 09 '15 19:09

dovid


2 Answers

System.Windows.Controls.ComboBox inherits from System.Windows.Controls.Primitives.Selector, which has a Selected attached event.

An attached event allows you to attach a handler for a particular event to some child element rather than to the parent that actually defines the event, even though neither the object potentially raising the event nor the destination handling instance define or otherwise "own" that event in their namespace.

System.Windows.Controls.Primitives.Selector has a SelectedEvent field, which backs the Selected attached event.

The reason for the Selected event is so that you can add one event handler to a control that will be executed when any of the many child controls raise the event. It is much more convenient than attaching event handlers to each of the ComboBoxItem's.

As for why it is included in the XAML code editor's intellisense; I believe that is a bug. The Selector class does have a public field for the selected event:

public static readonly RoutedEvent SelectedEvent = EventManager.RegisterRoutedEvent( 
            "Selected", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Selector));

But you can't add a handler for the event because the Selector class doesn't define the event like this:

public event RoutedEventHandler Selected
{ 
    add 
    {
        AddHandler(SelectedEvent, value); 
    }
    remove
    {
        RemoveHandler(SelectedEvent, value); 
    }
}

So it doesn't make sense for it to be included in the intellisense for ComboBox.

But, ListBoxItem, which is the base class for ComboBoxItem does define the event for you to attach handlers, so you can attach handlers to the ComboBoxItems

like image 85
Glen Thomas Avatar answered Oct 05 '22 05:10

Glen Thomas


Selected is attached event defined in Selector class and is supposed to be used with Selectors items. Usually you would write:

<ListBoxItem Selector.Selected="OnSelected" />

As shown below, ComboBox can be also an item, that's why there is the Selected event listed in intellisense:

<ListBox x:Name="Selector">
    <ComboBox x:Name="Item1" />
    <ComboBox x:Name="Item2" />
</ListBox>

However, combobox must be an item of another selector in order to use the Selected event:

<ComboBox x:Name="Selector1" Selector.Selected="ThisDoesNotWork"/>
<ListBox x:Name="Selector2">
    <ComboBox x:Name="Item1" Selector.Selected="ThisShouldWorkButDoesnt"/>
     <!-- See edit -->
    <ComboBox x:Name="Item2" />
</ListBox>

and the reason, why you dont have to write Selector.Selected=".." but just Selected="..."? Because Combobox is also selector. Similarily, you dont have to write Grid.Column but just Column, when attaching to Grid:

<Grid>
    <!-- Grid.Column="1" -->
    <Grid Column="1" />
</Grid>

At least, this this explain behavior of autocomplete in visual studio.

EDIT: neither Selector.Selected nor Selected=".." syntax in controls inherited from Selector does not compile, probably due to bug in XAML parser. But there is workaround using named xml namespace:

<ListBox x:Name="Selector2"
         xmlns:p="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework">
    <ComboBox x:Name="Item1" p:Selector.Selected="Item1_Selected"/>
like image 3
Liero Avatar answered Oct 05 '22 07:10

Liero