Get ListBox SelectedItem In RadWindow (all in code behind)

2 posts, 0 answers
  1. Jonathon
    Jonathon avatar
    50 posts
    Member since:
    Aug 2011

    Posted 26 Oct 2011 Link to this post

    Hi,

    I have a RadWindow that is displayed when a user clicks a button. In that RadWindow I have a ListBox and a Button. I want the first item in the ListBox to be selected by default, and then when they click the button, I will get the value of the ListBox item. Here is a general layout of the code I have so far:

    RadWindow userSelectionWindow = new RadWindow();
                StackPanel radWindowContent = new StackPanel();
                ListBox listBox = new ListBox();
                foreach (var server in Servers)
                {
                    ListBoxItem option = new ListBoxItem();
                    option.Content = server.Name;
                    listBox.Items.Add(option);
                }
     
                Binding selected = new Binding();
                selected.Mode = BindingMode.OneTime;
                selected.Source = SelectedItem;
                listBox.SetBinding(Selector.SelectedItemProperty, selected);
                radWindowContent.Children.Add(listBox);
     
                RadButton loginButton = new RadButton();
                loginButton.Command = new RelayCommand(CallMethod);
                loginButton.Content = "Log in";
                loginButton.Margin = new Thickness(0, 15, 0, 0);
                StyleManager.SetTheme(loginButton, new MetroTheme());
                radWindowContent.Children.Add(loginButton);
     
                userSelectionWindow.Content = radWindowContent;
                userSelectionWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                userSelectionWindow.CanMove = false;
                userSelectionWindow.ResizeMode = ResizeMode.NoResize;
                StyleManager.SetTheme(userSelectionWindow, new MetroTheme());
                userSelectionWindow.Show();

    For some reason my string SelectedItem is never getting updated. Am my going about this the wrong way? What I need is the value of the SelectedItem in the ListBox after the user clicks the Log In button. I can do the functionality I need in XAML without the RadWindow, but doing all this in code behind is confusing.

    Thanks,

    Jonathon
  2. George
    Admin
    George avatar
    1332 posts

    Posted 27 Oct 2011 Link to this post

    Hello Jonathon,

    I would suggest using the RadWindow control as a user control - please, refer to the following link - http://www.telerik.com/help/silverlight/radwindow-how-to-use-radwindow-as-user-control.html. In this way, you could define the content in the xaml.

    Hope this helps.

    Kind regards,
    George
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
Back to Top