Skip to main content

Microsoft Silverlight

Answered Question ComboBox.SelectedItem will not bind when DataContext setRSS Feed

(0)

rbaumannerb
rbaumannerb

Member

Member

0 points

1 Posts

ComboBox.SelectedItem will not bind when DataContext set

I'm trying to get my ComboBox to bind to an object property in my view model.  When the form loads the view model is passed to the constructor and is set to the user control's DataContext property.  The view model raises an event after it populates its properties with data it retrieves from a web service.  The user control subscribes to the event and performs some GUI stuff (enabling/disabling/formatting controls based on certain values).  After the data is loaded onto the form the ComboBoxes do not have selected items.  The corresponding properties in the view model have values for the ComboBoxes.  The boxes have the lists of values, with the correct object member displayed as the text.

Here's an example of the XAML used to create a ComboBox:

<ComboBox HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="1" Grid.Column="5" Background="#FFFAEE04" MinWidth="200" Margin="0,0,5,0" x:Name="cboPayType" ItemsSource="{Binding PayTypes}" DisplayMemberPath="Text" SelectedItem="{Binding PayType, Mode=TwoWay}"/>

My view model has a property called "PayType" that is an object with a Text, Value, and AdditionalValue member.

The user control's code-behind sets this.DataContext = vm; inside the event raised by the view model after its properties are set.  I also tried setting the DataContext of the user control's container grid to the vm...but that doesn't work either.  In addition to single ComboBoxes in the user control, there is also a grid that contains rows of data from another property in the view model.  One of the columns in the grid is a ComboBox.  This also displays the list of items, but does not show the SelectedItem value when the DataContext is set.  However, if I add a new row to the grid by clicking an "Add" button (which adds a new item to the collection that's bound to the grid) the default SelectedItem of the new row is displayed.

It seems as though I have a problem with binding my ComboBoxes when the form is loaded.  Has anyone else experienced this, and/or do you have a solution?

Mog Liang - MSFT
Mog Lian...

All-Star

All-Star

15874 points

1,541 Posts

Answered Question

Re: ComboBox.SelectedItem will not bind when DataContext set

Hi,

I cannot repro your issue in silverlight3, selecteditem binding works correct as well as itemssource binding.

one possible reason: selecteditem is not object reference contained in itemssource.

my test code here, have a try

entity

   public class News
    {
        public string Title { set; get; }
        public DateTime Date { set; get; }
        public int Importance { set; get; }
        public NewsContent Content { set; get; }
    }

    public class NewsContent
    {
        public string Content1 { set; get; }
        public string Content2 { set; get; }
        public string Content3 { set; get; }
    }

    public class NewsList : List
    {
        public NewsList():base()
        {
            for (int i = 0; i < 10; i++)
            {
                Add(new News
                {
                    Title = "title:" + i,
                    Date = DateTime.Now.AddHours(1),
                    Importance = i % 3,
                    Content = new NewsContent
                    {
                        Content1 = "Content1",
                        Content2 = "Content2",
                        Content3 = "Content3"
                    }
                });
            }
        }
    }
 Model 
    public class NewsModel
    {
        public NewsList List { set; get; }
        public News Selected { set; get; }
        public NewsModel()
        {
            List = new NewsList();
            Selected = List[5]; // selected is one object contained in list
        }
    }

 View (xaml) 

    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel>
            <ComboBox Name="cb1" ItemsSource="{Binding List}" DisplayMemberPath="Title" SelectedItem="{Binding Selected,Mode=TwoWay}"/>
            <Button Content="click me" Click="Button_Click"/>
        </StackPanel>
    </Grid>
 (code) 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.DataContext = new NewsModel();
        }

Mog Liang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities