Set a button as "default"

2 posts, 0 answers
  1. Berthold
    Berthold avatar
    38 posts
    Member since:
    Jul 2010

    Posted 22 Nov 2010 Link to this post

    Hi,

    is it somehow possible (maybe via settings?) to define a rad-button as default button of a silverlight page?
    It should be called whenever enter is pressed

    Thanks
    Berthold
  2. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 23 Nov 2010 Link to this post

    Hello Berthold,

    Currently RadButton for Silverlight does not support the IsDefault functionality as seen in WPF. There is a nice workaround which you can apply (courtesy of Patrick Cauldwell):

    <UserControl x:Class="SilverlightApplication2.MainPage"
            xmlns:local="clr-namespace:SilverlightApplication2"
            xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <StackPanel>
                <TextBox local:DefaultButtonService.DefaultButton="buttonOK"/>
                <RadioButton Content="RadioButton" local:DefaultButtonService.DefaultButton="buttonOK"/>
                <telerik:RadButton Content="OK" x:Name="buttonOK" Click="buttonOK_Click"/>
            </StackPanel>
            <ListBox x:Name="log" Grid.Column="1" />
        </Grid>
    </UserControl>

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
     
        private void buttonOK_Click(object sender, RoutedEventArgs e)
        {
            this.log.Items.Add("[" + DateTime.Now.ToLongTimeString() + "] button clicked");
        }
    }
     
    public static class DefaultButtonService
    {
        public static readonly DependencyProperty DefaultButtonProperty =
            DependencyProperty.RegisterAttached("DefaultButton", typeof(string), typeof(DefaultButtonService), new PropertyMetadata(OnDefaultButtonChanged));
     
     
        public static string GetDefaultButton(DependencyObject d)
        {
            return (string)d.GetValue(DefaultButtonProperty);
        }
     
        /// <summary>
        /// Sets the CommandParameter property.
        /// </summary>
        public static void SetDefaultButton(DependencyObject d, string value)
        {
            d.SetValue(DefaultButtonProperty, value);
        }
     
        private static void OnDefaultButtonChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement tb = d as FrameworkElement;
            if (tb != null)
            {
                tb.KeyUp += TextBox_KeyUp;
            }
        }
     
        static void TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Enter:
                    string name = (string)((DependencyObject)sender).GetValue(DefaultButtonProperty);
                    object root = App.Current.RootVisual;
                    object button = ((FrameworkElement)root).FindName(name);
                    if (button is Button)
                    {
                        ButtonAutomationPeer peer = new ButtonAutomationPeer((Button)button);
     
                        IInvokeProvider ip = (IInvokeProvider)peer;
                        ip.Invoke();
                    }
                    break;
            }
        }
    }

    Give it a try and let me know how it works for you.

    Sincerely yours,
    Kiril Stanoev
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  3. DevCraft banner
Back to Top