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


    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

  2. Kiril Stanoev
    Kiril Stanoev avatar
    1512 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"
        <Grid x:Name="LayoutRoot" Background="White">
                <ColumnDefinition />
                <ColumnDefinition />
                <TextBox local:DefaultButtonService.DefaultButton="buttonOK"/>
                <RadioButton Content="RadioButton" local:DefaultButtonService.DefaultButton="buttonOK"/>
                <telerik:RadButton Content="OK" x:Name="buttonOK" Click="buttonOK_Click"/>
            <ListBox x:Name="log" Grid.Column="1" />

    public partial class MainPage : UserControl
        public MainPage()
        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;

    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
Back to Top