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

Get ListBox SelectedItem In RadWindow (all in code behind)

1 Answer 86 Views
Window
This is a migrated thread and some comments may be shown as answers.
Jonathon
Top achievements
Rank 1
Jonathon asked on 27 Oct 2011, 12:27 AM
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

1 Answer, 1 is accepted

Sort by
0
George
Telerik team
answered on 27 Oct 2011, 08:24 AM
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 >>

Tags
Window
Asked by
Jonathon
Top achievements
Rank 1
Answers by
George
Telerik team
Share this question
or