This is a migrated thread and some comments may be shown as answers.

Context Menu

3 Answers 81 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 27 Feb 2019, 07:28 PM

I want to add further command to the context menu that appears when right-clicking on the RadAutoCompleteBox.

When I add a context menu flyout it appears in addition to the default context menu ("Copy, Paste, …"). This is confusing for the end-user.

I found many articles handling this question. In many cases it was suggested to influence the visual structure. But this seems not to be possible with Telerik UI for UWP.

Is there any way to influence, or to abandon the default menu? It would also be an option for me just to show my context menu.

3 Answers, 1 is accepted

Sort by
0
Accepted
Lance | Manager Technical Support
Telerik team
answered on 27 Feb 2019, 08:16 PM
Hi Thomas,

I would open an Issue on the GitHub for UI for UWP. That way the developers can assist with overriding or canceling the built-in ContextMenu, this needs a property to be implemented if you don't want to implement a custom Style and override the ControlTemplate.

Regards,
Lance | Technical Support Engineer, Principal
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Lance | Manager Technical Support
Telerik team
answered on 27 Feb 2019, 08:55 PM
Hello Thomas,

I've spent a little time on this and wrote you a custom Visual Tree scraper that lets you find the internal TextBox (AutoCompleteTextBox) used by the RadAutoCompleteBox.  This the actual control that has the context menu.

Once you have a reference to that internal TextBox control, you can then subscribe to the ContentMenuOpening event handler and cancel it.

Here's the approach in action:

<input:RadAutoCompleteBox x:Name="radAutoCompleteBox"
                          Loaded="RadAutoCompleteBox_OnLoaded"
                          Unloaded="RadAutoCompleteBox_OnUnloaded"
                          Width="200" VerticalAlignment="Center">
    <input:RadAutoCompleteBox.ContextFlyout>
        <Flyout>
            <TextBlock Text="My Custom Context Menu"/>
        </Flyout>
    </input:RadAutoCompleteBox.ContextFlyout>
</input:RadAutoCompleteBox>

/// Traverse the child visual tree to find the AutoCompleteTextBox
/// and SUBSCRIBE the ContextMenuOpening event handler
private void RadAutoCompleteBox_OnLoaded(object sender, RoutedEventArgs e)
{
    var results = new List<AutoCompleteTextBox>();
    Extensions.FindChildren<AutoCompleteTextBox>(results, radAutoCompleteBox);
    AutoCompleteTextBox internalTextBox = results.FirstOrDefault();
 
    // subscribe
    internalTextBox.ContextMenuOpening += InternalTextBox_ContextMenuOpening;
}
 
/// Traverse the child visual tree to find the AutoCompleteTextBox
/// and UNSUBSCRIBE the ContextMenuOpening event handler
private void RadAutoCompleteBox_OnUnloaded(object sender, RoutedEventArgs e)
{
    var results = new List<AutoCompleteTextBox>();
    Extensions.FindChildren<AutoCompleteTextBox>(results, radAutoCompleteBox);
    AutoCompleteTextBox internalTextBox = results.FirstOrDefault();
 
    // Unsubscribe
    internalTextBox.ContextMenuOpening -= InternalTextBox_ContextMenuOpening;
}
 
private void InternalTextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
    // Cancel the context menu
    e.Handled = true;
}


This will result in only your custom ContextMenu on the highest level on the component to show.

Regards,
Lance | Technical Support Engineer, Principal
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Thomas
Top achievements
Rank 1
answered on 03 Mar 2019, 01:03 PM

Hi Lance,

Thanks! This solved my problem. As I could not directly access Extensions.FindChildren, I implemented it as follows:

Private Sub OnAuthors_Loaded(sender As Object, e As RoutedEventArgs)
    _currentAuthorBox = DirectCast(sender, RadAutoCompleteBox)
    Dim descendants = _currentAuthorBox.FindDescendants(Of AutoCompleteTextBox)
    _internalTextBox = descendants.FirstOrDefault()
 
    AddHandler _internalTextBox.ContextMenuOpening, AddressOf InternalTextBox_ContextMenuOpening
End Sub

 

Best Regards,

Thomas,

Tags
AutoCompleteBox
Asked by
Thomas
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Thomas
Top achievements
Rank 1
Share this question
or