1

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#?

zack
  • 7,115
  • 14
  • 53
  • 63
  • What problems are you having? Wouldn't in XAML or "myComboBox.SelectedIndex=0" in code-behind be good enough? – ASanch Sep 02 '10 at 16:50
  • If you send sample code it would be easier to determine, often I have a selected value property in my viewmodel, and it is programmatically set. Are you doing this using MVVM pattern? – JoshVarga Sep 02 '10 at 16:54
  • That did not work so I posted this question here. I am new to WPF but I did a similar thing in Windows forms with .NET 3.5. I know this should not be such a problem but it just does not work. – zack Sep 02 '10 at 16:55

4 Answers4

0

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
  • 12,236
  • 9
  • 39
  • 44
  • 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.. – zack Sep 02 '10 at 17:35
0

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

myName.SelectedIndex = 0;

Sun
  • 21
  • 1
0

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
  • 713
  • 1
  • 7
  • 20
0

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
  • 11,444
  • 12
  • 50
  • 64