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

RadMenu Problem?

5 Answers 203 Views
Menu
This is a migrated thread and some comments may be shown as answers.
sundhar
Top achievements
Rank 1
sundhar asked on 06 Apr 2011, 03:01 PM
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.....
 

5 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 08 Apr 2011, 10:11 AM
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
0
sundhar
Top achievements
Rank 1
answered on 08 Apr 2011, 01:07 PM
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?
0
Hristo
Telerik team
answered on 11 Apr 2011, 10:13 AM
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
0
sundhar
Top achievements
Rank 1
answered on 12 Apr 2011, 01:43 PM
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...
0
Pana
Telerik team
answered on 18 Apr 2011, 07:24 AM
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
Tags
Menu
Asked by
sundhar
Top achievements
Rank 1
Answers by
Hristo
Telerik team
sundhar
Top achievements
Rank 1
Pana
Telerik team
Share this question
or