RadMenu Problem?

6 posts, 0 answers
  1. sundhar
    sundhar avatar
    12 posts
    Member since:
    Mar 2011

    Posted 06 Apr 2011 Link to this post

    Hello Telerik Team,
                                  I have one problem in my WPF project.Let i explain in detail

    In my UI contains three TextBoxes..
    <Label Content="First _name:" HorizontalAlignment="Right" Margin="0,40,408,247" Target="{Binding ElementName=firstNameTxt}" />
            <TextBox Margin="101,40,101,247" Name="firstNameTxt" Text="{Binding Path=FirstName, ValidatesOnDataErrors=True, =LostFocus}"  />
            <Label Content="_Last name:" HorizontalAlignment="Right" Margin="0,72,408,216" Target="{Binding ElementName=lastNameTxt}" />
            <TextBox Margin="101,72,101,216" Name="lastNameTxt" Text="{Binding Path=LastName, ValidatesOnDataErrors=True, UpdateSourceTrigger=LostFocus}"  />
            <Label Content="E-_mail:" HorizontalAlignment="Right" Margin="0,104,408,184" Target="{Binding ElementName=emailTxt}" />
            <TextBox Margin="101,104,101,184" Name="emailTxt" Text="{Binding Path=Email, ValidatesOnDataErrors=True, UpdateSourceTrigger=LostFocus}" />
    All are set into UpdateSourceTrigger=LostFocus;
    and also i have standard menu bar
    <Menu Height="23" Name="menu1" VerticalAlignment="Top">
                <MenuItem Header="File">
                    <MenuItem Header="Save" Click="MenuItem_Click" />
                </MenuItem>
            </Menu>
    The menu bar purpose is saving the details to database..
    Every time after entered value in textbox atleast one time i change focus to another control then only
    the values are updated.Problem is if i enter third Textbox value mean here i change focus to second textbox or first textbox then only Third box value updated.

    Here i want to update third box value without changing focus to firstbox or second box.. 
    so i make some codes in Codebehind it comes,
    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
      
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                this.DataContext = Customer.CreateNewCustomer();
            }
      
            private void MenuItem_Click(object sender, RoutedEventArgs e)
            {
                  
                if (Keyboard.FocusedElement.GetType() == typeof(TextBox))
                {
                   BindingExpression exprLastName = ((TextBox)
                       Keyboard.FocusedElement).GetBindingExpression(TextBox.TextProperty);
                   exprLastName.UpdateSource();
                }
                MessageBox.Show("Passed\n" +
                        ((Customer)this.DataContext).FirstName  + "\n" +
                        ((Customer)this.DataContext).LastName  + "\n" +
                        ((Customer)this.DataContext).Email);
             }
        }
    The Binding expression binding value when still focus in third TextBox also...(not leaveing focus to other control)

    after that i am clicking save in menuItem all values are comes correctly.Third value also updated.

    After that i am changing standard menubar to RadMenu .
    Here also i am doing same process.but its not return thid textbox value..

    I realize where is the mistake comes from 
            if suppose i am using standard menu and the focus is in third textBox
    that time i am clicking save means inside save method comes Keyboard.FocusElement=TextBox  (finding using debugging)

            same time if i am using Radmenu and the focus is in third textbox
    that time i am clicking save means inside save method comes Keyboard.FocusElement=Save will come not TextBox..

    I don't know why Telerik.wimdows.controls.Radmenu Header.save come instead of Textbox
    can anybody give correct solution? 
         give me solution in detail..
    Thank you.....
     
  2. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 08 Apr 2011 Link to this post

    Hello sundhar,

    This is because Menu control and RadMenu control implementation is different.
    MenuItem control raise Click event after one dispatcher (e.g. not instantly) while RadMenuItem raise Click immediately. This is why the focused element is still RadMenuItem.
    You can workaround this behavior using the dispatcher like this:
    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        this.Dispatcher.BeginInvoke(new Action(() =>
        {
            if (Keyboard.FocusedElement.GetType() == typeof(TextBox))
            {
                BindingExpression exprLastName = ((TextBox)
                    Keyboard.FocusedElement).GetBindingExpression(TextBox.TextProperty);
                exprLastName.UpdateSource();
            }
            MessageBox.Show("Passed\n" +
                    ((Customer)this.DataContext).FirstName + "\n" +
                    ((Customer)this.DataContext).LastName + "\n" +
                    ((Customer)this.DataContext).Email);
        }));
    }

    Let us know if this helps you.

    All the best,
    Hristo
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. sundhar
    sundhar avatar
    12 posts
    Member since:
    Mar 2011

    Posted 08 Apr 2011 Link to this post

    Hello Hristo,
                       Thanks for your reply..
    actually its working fine in Standard menu... But  not in Radmenu..
    i did  check your code too... keyboard.FocusedElement.GetType() value is still 
    Telerik.wimdows.controls.Radmenu Header.save (finding using debugging).
    Here i want Textbox only...

    <telerik:RadMenu Name="radMenu1" NotifyOnHeaderClick="True" Width="auto" Height="26" DockPanel.Dock="Top" FontSize="11" FontFamily="Verdana" ItemClick="radMenu1_ItemClick" ClickToOpen="False">
        <telerik:RadMenuItem Focusable="False" Header="File">
             <telerik:RadMenuItem Header="_Save" Focusable="False" Name="Save" Command="{x:Static custom:MainWindow.crudcommand}" CommandParameter="Save">
        </telerik:RadMenuItem
    </telerik:RadMenu>

    If i am using Focusable="False" mean its working fine..same time keyboard navigation not working..(see above XAML)

    My problem is value should Update when focus is TextBox when UpdateSourceTrigger=LosFocus in XAML..

    can you explain more details?
  5. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 11 Apr 2011 Link to this post

    Hi sundhar,

    I've attached sample project demonstrating that using Dispatcher.BeginInvoke you could get the Focused textbox and manually update its binding.

    Is you don't want to use this workaround then the easiest way will be to change the UpdateSourceTrigger to PropertyChanged (instead of LostFocus). This way there will be no need to manually update the binding.

    If you set Focusable to False then RadMenuItem cannot be focused and cannot receive keyboard input. That is why keyboard navigation is not working (which is expected).

    Let us know if you need more details.

    Kind regards,
    Hristo
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  6. sundhar
    sundhar avatar
    12 posts
    Member since:
    Mar 2011

    Posted 12 Apr 2011 Link to this post

    Hello Hristo,
                           Thanks ..
    Its working Fine ...
    same way i am using RadcomboBox But Its not working..Here i am getting Keyboard.FocusedElement.GetType() =PickerTextBox;

    How to Update RadcomboBox value Explicitly..
    Thanks for advance...
  7. Pana
    Admin
    Pana avatar
    748 posts

    Posted 18 Apr 2011 Link to this post

    Hello,

    I would not recommend the following approach but I can find other solution for your problem.

    RadComboBox has several controls inside its ControlTemplate - TextBox (PickerTextBox), ToggleButton (the drop down button), Popup, Clear Button etc. When you click in the RadComboBox you actually focus the PickerTextBox inside. To get the RadComboBox that the picker belongs to you can use the ParentOfType<RadComboBox>() extension method from Telerik.Windows.Controls. For example:

    UIElement pickerTextBox = Keyboard.FocusedElement.GetType() as UIElement;
    if (pickerTextBox != null)
    {
        RadComboBox combo = pickerTextBox.ParentOfType<RadComboBox>();
        if (combo != null)
        {
            // Do some work with the combo
        }
    }

    Regards,
    Pana
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
Back to Top
UI for WPF is Visual Studio 2017 Ready