FocusedElement is always getting null

2 posts, 0 answers
  1. Uddhao
    Uddhao avatar
    6 posts
    Member since:
    Nov 2014

    Posted 11 Feb Link to this post

    Hello,

    I using RadDataBoundListBox in mvvmcross WP app. I using Pivot for tabs, previously i have one viewmodel for all pivot item but now created child view models for all pivot items. Since i use DataContext property for child view model binging but getting problem to RadContextMenu. when Opening event fire of context menu then FocusedElement getting null .

      tktTappedItem is always null in this line.

      RadDataBoundListBoxItem tktTappedItem = e.FocusedElement as Telerik.Windows.Controls.RadDataBoundListBoxItem;

    This is my code :

    - test.xaml file :

      <phone:PivotItem x:Name="uiPivotTimeEntry" DataContext="{Binding TicketTimeEntryVM}" >
                    <phone:PivotItem.Header>
                        <StackPanel Orientation="Horizontal">

                            <telerik:RadImageButton HorizontalAlignment="Center"  Margin="0, 20, 0, 0" VerticalAlignment="Center" Command="{Binding CmdTimeEntryAdd}" ButtonShape="Ellipse" RestStateImageSource="/Assets/Images/Add-New.png"/>

                            <TextBlock Text="time entries" />
                        

                        </StackPanel>

                    </phone:PivotItem.Header>

                    <telerik:RadDataBoundListBox x:Name="uiTimeEntryList" ItemsSource="{Binding TicketTimeEntries}" ItemTemplate="{StaticResource TimeEntryItemTemplate}" EmptyContent="There are no time entries to display">

                        <telerikPrimitives:RadContextMenu.ContextMenu>
                            <telerikPrimitives:RadContextMenu OpenGesture="Hold" Opening="uiTicketTimeEntryMenu_Opening" >
                                <telerikPrimitives:RadContextMenuItem Content="edit" Tap="onTicketTimeEntryEdit_Tap"  />
                                <telerikPrimitives:RadContextMenuItem Content="delete" Tap="onTicketTimeEntryDelete_Tap" />
                            </telerikPrimitives:RadContextMenu>
                        </telerikPrimitives:RadContextMenu.ContextMenu>

                    </telerik:RadDataBoundListBox>
                </phone:PivotItem>

     

    - test.xaml.cs

    public partial class TicketEditHomeView : MvxPhonePage
        {
           public TicketEditHomeView()
            {
                InitializeComponent();

                // Set the Context menu...
              
                RadContextMenu.SetFocusedElementType(this.uiTimeEntryList, typeof(RadDataBoundListBox));
             }
           
             
            /// <summary>
            /// TicketTimeEntry context menu opening
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void uiTicketTimeEntryMenu_Opening(object sender, Telerik.Windows.Controls.ContextMenuOpeningEventArgs e)
            {
                try
                {
                    // This tktTappedItem is getting null
                    RadDataBoundListBoxItem tktTappedItem = e.FocusedElement as Telerik.Windows.Controls.RadDataBoundListBoxItem;
                    if (tktTappedItem != null)
                    {
                        ticketTimeEntryDataContext = tktTappedItem.DataContext;
                    }


                    if (tktTappedItem == null)
                    {
                        e.Cancel = true;
                    }
                }
                catch { }
                finally
                {
                    if (tktTappedItem != null)
                    {
                        tktTappedItem = null;
                    }
                }
            }


            /// <summary>
            /// Fire when click on Edit menu item
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void onTicketTimeEntryEdit_Tap(object sender, System.Windows.Input.GestureEventArgs e)
            {
                this.ViewModel.TicketTimeEntryVM.CmdTimeEntrySelected.Execute(ticketTimeEntryDataContext);
            }


            /// <summary>
            /// Fire when click on Delete menu item
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void onTicketTimeEntryDelete_Tap(object sender, System.Windows.Input.GestureEventArgs e)
            {
                this.ViewModel.TicketTimeEntryVM.CmdTimeEntryDeleted.Execute(ticketTimeEntryDataContext);
            }
     }

    Thank you.

  2. Uddhao
    Uddhao avatar
    6 posts
    Member since:
    Nov 2014

    Posted 15 Feb Link to this post

    -I figured out where i am going wrong. While setting focused element type in constructor i am using RadDataBoundListBox instead of RadDataBoundListBoxItem.

    Here is right code :
    public TicketEditHomeView()

    {

    InitializeComponent();

    // Set the Context menu...

    RadContextMenu.SetFocusedElementType(this.uiTimeEntryList, typeof(RadDataBoundListBoxItem));

    }

    -I figured out where i am going wrong. While setting focused element type in constructor i am using RadDataBoundListBox instead of RadDataBoundListBoxItem.

    Here is right code :

     public TicketEditHomeView()
    {
        InitializeComponent();
    
        // Set the Context menu...
    
        RadContextMenu.SetFocusedElementType(this.uiTimeEntryList, typeof(RadDataBoundListBoxItem));
     }

    -I figured out where i am going wrong. While setting focused element type in constructor i am using RadDataBoundListBox instead of RadDataBoundListBoxItem.

    Here is right code :

     public TicketEditHomeView()
    {
        InitializeComponent();
    
        // Set the Context menu...
    
        RadContextMenu.SetFocusedElementType(this.uiTimeEntryList, typeof(RadDataBoundListBoxItem));
     }

    -I figured out where i am going wrong. While setting focused element type in constructor i am using RadDataBoundListBox instead of RadDataBoundListBoxItem.

    Here is right code :

     public TicketEditHomeView()
    {
        InitializeComponent();
    
        // Set the Context menu...
    
        RadContextMenu.SetFocusedElementType(this.uiTimeEntryList, typeof(RadDataBoundListBoxItem));
     }

    -I figured out where i am going wrong. While setting focused element type in constructor i am using RadDataBoundListBox instead of RadDataBoundListBoxItem.

    Here is right code :

     public TicketEditHomeView()
    {
        InitializeComponent();
    
        // Set the Context menu...
    
        RadContextMenu.SetFocusedElementType(this.uiTimeEntryList, typeof(RadDataBoundListBoxItem));
     }
  3. DevCraft banner
Back to Top