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

Set a button as "default"

1 Answer 123 Views
Buttons
This is a migrated thread and some comments may be shown as answers.
Berthold
Top achievements
Rank 1
Berthold asked on 22 Nov 2010, 05:26 PM
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

1 Answer, 1 is accepted

Sort by
0
Kiril Stanoev
Telerik team
answered on 23 Nov 2010, 04:13 PM
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
Tags
Buttons
Asked by
Berthold
Top achievements
Rank 1
Answers by
Kiril Stanoev
Telerik team
Share this question
or