views:

253

answers:

4

I am working on WPF. I am using visual studio 2010, .NET 4.0 and using a Radcombobox in my application. It is getting populated correctly with 3 strings in it and now I am having trouble choosing a default value. I want to select the first index value as the default value when it starts up loading the 3 strings in the combo box drop down. How do i do this programmatically? Should this be done in xaml or in C#?

A: 

Use a counter. Create a boolean variable and call it boolDefaultSet. It should initialize with a default value of false.

Presumably you would use a loop to output the option boxes for your select box... inside that loop write some logic to check whether boolDefaultSet is true. If not, then print the option box with an attribute of 'selected', then set boolDefaultSet equal to true. Here is some vb code that you could implement in C#:

Dim boolDefaultSet as boolean
for i as integer = 0 to 2
    if boolDefaultSet then
        Response.Write("<option value="+myval+">"+myval+"</option>")
    else
        Response.Write("<option value="+myval+" selected>"+myval+"</option>")
        boolDefaultSet=true
next i

Adam
I mean do I really need to do this? There should be an attribute or a property of the RadComboBox right ? Shouldnt it be that simple? I do not know why SelectedItem = 1 in the combo box tag in xaml doesnt seem to work. Or in C# code either..
VP
SelectedItem won't work. Try SelectedIndex
GWLlosa
A: 

If setting SelectedIndex from XAML was not working I would try to do it in C# code load event.

myName.SelectedIndex = 0;

Sun
that wouldnt work either
VP
A: 

You shouldn't use SelectedValue AND SelectedIndex at the same time since it often creates a sort of conflict. I suggest you remove SelectedIndex="0" from the xaml and set the property to which you bind your SelectedValue from the code.

example:

MySelectedValue = MyItemsSource[0];
Captain
A: 

This is more of an MVVM answer:

Bind the SelectedIndex property in XAML to a property on your ViewModel.

SelectedIndex="{Binding Path=SelectedIndex, Mode=TwoWay}"

In your ViewModel, set the SelectedIndex value to 1 and then call PropertyChangeNotification so the view knows to update (this assumes your ViewModel implements INotifyPropertyChanged, and most VM implementations use a base class to do this).

SelectedIndex = 1;
NotifyPropertyChanged("SelectedIndex");
Chris Holmes