开发者

Why does the SelectedItem not display in combobox

With the below binding in XAML, the selected item is not displayed in a combobox (specifically, the current type is not displayed for a site). We are using the caliburn micro framework and DevForce Ideablade. MySite is an Ideablade entity.

<ComboBox Grid.Column="3" Grid.Row="3" 
          ItemsSource="{Binding MySiteTypes}"
          DisplayMemberPath="description"
          SelectedItem="{Binding MySite.SiteType, Mode=TwoWay}"
          SelectedValuePath="description"/>

Code in the ViewModel:

    private BindableCollection<SiteType> _mySiteTypes;
    public BindableCollection<SiteType> MySiteTypes
    {
        get { return _mySiteTypes; }
        set { _mySiteTypes = value; NotifyOfPropertyChange(() => MySiteTypes); }
    }

Once I set the site type with the combobox (the typ开发者_开发技巧es are present in the dropdown), it correctly displays the type and changes the type in the database. What am I missing, why doesn't it want to display the existing type the first time?


From my experience with the ComboBox these kind of issues often boil down to timing. Is your ItemSource (MySiteTypes) available before the SelectedItem binding is evaluated?


Ok, I found the solution (Myles J was close):

The selected item is evaluated before the items from the itemsource were loaded causing the selected item not to be displayed. I needed to call NotifyOfPropertyChange for MySite AFTER the async query completed that populates MySiteTypes.


Are the items loaded? Maybe you'll have to use a DataTemplate to show their text property.

If they are strings, I had a similar problem recently and had to use the selectedIndex by using a converter:

Converter for ComboBox in Silverlight 4

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜