Why this UWP ComboBox can be blank after initialization but works fine for selection?

一笑奈何 提交于 2020-01-21 09:02:05

问题


I have a ComboBox like this

<ComboBox
    Grid.Column="1"
    Padding="5,0,0,0"
    DisplayMemberPath="Description" 
    SelectedItem="{Binding MaxXXAge, Mode=TwoWay, Converter={StaticResource MaxXXAgeToMaxXXAgeMemberConverter}}"
    ItemsSource="{Binding ElementName=SettingsXXScrollViewer, Path=DataContext.MaxXXAgeMemberGroup, Mode=OneWay}" />

However, after initialization, the combobox is blank. It actually works fine after this. I can select and show the selected item as expected. It's just the first glance doesn't work. However, I already initialized MaxXXAge and the converter has been triggered. Here is the group

public IReadOnlyList<MaxXXAgeMembers> MaxXXAgeMemberGroup { get { return MaxXXAgeMembers.Options; } }

And this is the definition for MaxXXAgeMembers

public class MaxXXAgeMembers
        {
            public MaxXXAge MaxXXAge { get; private set; }
            public string Description { get; private set; }

            public static readonly IReadOnlyList<MaxXXAgeMembers> Options = new ReadOnlyCollection<MaxXXAgeMembers>(new[]
            {
                new MaxXXAgeMembers { MaxXXAge =  MaxXXAge.OneDay, Description = Strings.SettingSync_OneDay},
.......
            });

            public static MaxXXAgeMembers FromMaxXXAge(MaxXXAge maxXXAge)
            {
                return Options.First(option => option.MaxXXAge == maxXXAge);
            }
        }

//Added the Overriding Equals later

public override bool Equals(object obj)
{
     if (obj == null || !(obj is MaxEmailAgeMembers))
          return false;
     return ((MaxEmailAgeMembers)obj).Description.Equals(this.Description);
}

public override int GetHashCode()
{
     return this.Description.GetHashCode();
}

The converter is like this

public sealed class MaxEmailAgeToMaxEmailAgeMemberConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return WPSettingsEmailViewModel.MaxEmailAgeMembers.FromMaxEmailAge((MaxEmailAge)value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return ((WPSettingsEmailViewModel.MaxEmailAgeMembers)value).MaxEmailAge;
    }
}

Any idea?


回答1:


It's blank because you don't have anything selected in the first place. If I'm not mistaken, you have to either use SelectedItem to bind your selection or SelectedValue with SelectedValuePath.

I actually never use SelectedValue with SelectedValuePath myself, so after initializing collection of items which ComboBox.ItemSource will be binded to - for example ObservableCollection<Person> Persons {get; set;} - I also set selected item property Person SelectedPerson {get; set;} to one of the values from collection. Then I bind ComboBox.SelectedItem to this property, so on initialization it shows predefined selected value.

I guess you can achieve the same with SelectedValue and SelectedValuePath, but you have to use them together as described here.




回答2:


I used x:Bind for the ItemResource and added ViewModel inside code behind, and solved this problem.




回答3:


You ComboBox isn't blank, but it don't know how to render your MaxXXAgeMembers. You should use ItemTemplate to tell this to him. For Ex:

<ComboBox
    Grid.Column="1"
    Padding="5,0,0,0"
    SelectedValue="{Binding MaxXXAge, Mode=TwoWay, Converter={StaticResource MaxXXAgeToMaxXXAgeMemberConverter}}"
    ItemsSource="{Binding ElementName=SettingsXXScrollViewer, Path=DataContext.MaxXXAgeMemberGroup, Mode=OneWay}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Description}" /> 
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>


来源:https://stackoverflow.com/questions/39090923/why-this-uwp-combobox-can-be-blank-after-initialization-but-works-fine-for-selec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!