tags:

views:

305

answers:

3
<CombobBox x:Name="cbo" 
           Style="{StaticResource ComboStyle1}"
           DisplayMemberPath="NAME"
           SelectedItem="{Binding Path=NAME}"
           SelectedIndex="1">
  <ComboBox.ItemTemplate>
    <DataTemplate>
      <Grid>
        <TextBlock Text="{Binding Path=NAME}"/>
      </Grid>
    </DataTemplate>
  </ComboBox.ItemTemplate>
</ComboBox>

In Window OnLoaded event i written the code to set the ItemSource

cbo.ItemsSource = ser.GetCity().DefaultView;

While loading the window i can see that the initially the the first item is loading but on the same time it clear the displayed item. I am stuck up in this scenario and any help is appreciated.

Regards

Kishore

A: 

Please see the link below

http://asimsajjad.blogspot.com/2009/02/combobox-binding-in-wpf.html

Hope that will help.

Asim Sajjad
if iam giving the selectedindex then it will not showing the data. Binding and showing data wokrs perfect.Thnx
Kishore Kumar
A: 

Resetting the ItemsSource will mess up the selection.

Also, you are setting both SelectedItem and SelectedIndex. You want to set only one of these; if you set both, only one will take effect.

In addition, your SelectedItem declaration is probably wrong. SelectedItem="{Binding NAME}" will look for an item which is equal to the value of the NAME property of the ambient (probably Window-level) DataContext. This will work only if the ComboBox.ItemsSource is a list of strings. Since your ItemTemplate works, I assume ComboBox.ItemsSource is actually a list of City objects. But you are telling WPF that the SelectedItem should be a string (a NAME). This string will never be equal to any City object, so the result will be no selection.

So to fix the problem, after setting ItemsSource, set either SelectedItem or SelectedIndex, depending on your requirements and your data model:

cbo.ItemsSource = ser.GetCity().DefaultView;
cbo.SelectedIndex = 1;
// or: cbo.SelectedItem = "Wellington";    // if GetCity() returns strings - probably not
// or: cbo.SelectedItem = City.Wellington; // if GetCity() returns City objects
itowlson
+1  A: 

QUICK ANSWER : Set SelectedIndex = 1 from code-behind.

It seems that the code in XAML executes first (the InitializeComponent() method), which sets SelectedIndex = 1, but ItemsSource is not specified yet! So SelectedIndex won't affect! (And remember, you cannot specify ItemsSource before InitializeComponent())

So you have to manually set SelectedIndex = 1 after setting ItemsSource.


You should do like this :

XAML

            <ComboBox x:Name="cbo"
                      Style="{StaticResource ComboStyle1}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <TextBlock Text="{Binding Path=NAME}"/>
                        </Grid>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

Code

     cbo.ItemsSource = ser.GetCity().DefaultView;
     cbo.SelectedIndex = 1;

Or this:

XAML

            <ComboBox x:Name="cbo" Initialized="cbo_Initialized"
                      Style="{StaticResource ComboStyle1}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <TextBlock Text="{Binding Path=NAME}"/>
                        </Grid>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

Code

    private void cbo_Initialized(object sender, EventArgs e)
    {
        cbo.SelectedIndex = 1;
    }

Also note that i've removed DisplayMemberPath="NAME" because you cannot set both DisplayMemberPath and ItemTemplate at the same time. And also, use either SelectedItem or SelectedIndex, not both.

Mihir Gokani
Thanks a lot for your comments.
Kishore Kumar