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


    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;
                Binding selected = new Binding();
                selected.Mode = BindingMode.OneTime;
                selected.Source = SelectedItem;
                listBox.SetBinding(Selector.SelectedItemProperty, selected);
                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());
                userSelectionWindow.Content = radWindowContent;
                userSelectionWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                userSelectionWindow.CanMove = false;
                userSelectionWindow.ResizeMode = ResizeMode.NoResize;
                StyleManager.SetTheme(userSelectionWindow, new MetroTheme());

    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.


  2. George
    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 - In this way, you could define the content in the xaml.

    Hope this helps.

    Kind regards,
    the Telerik team

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

Back to Top