How to auto select first item? #611
-
// source:
SourceCache<CountryInfo, Guid> countryCache
// bindable collection:
[Bindable(true)]
ObservableCollectionExtended<CountryViewModel> CountryItems
// selected item:
[Bindable(true)]
CountryViewModel CurrentCountry
// bind
countryCache.Connect().Sort().Transform().ObserveOnDispatcher().Bind(CountryItems).Subscribe();
// then auto select first item default.
// warn, CountryItems is empty (can't wait for first data filling.)
CurrentCountry = CountryItems.FirstOrDefault(dd=>dd.Id == old_saved_id);
// xaml
<Combobox ItemsSource="{Binding CountryItems}" SelectedItem="{Binding CurrentCountry"/> How to wait first data filling for CountryItems? And get any item. |
Beta Was this translation helpful? Give feedback.
Answered by
RolandPheasant
Jun 27, 2022
Replies: 2 comments
-
test is here: please open SelectFirstItemViewModel module view, select a item, close it to save. |
Beta Was this translation helpful? Give feedback.
0 replies
-
There's a lot of options for selecting the first item. For example you can: private IDisposable CreateCountryBind()
{
var handle = dataAccessor.CountryCache.Connect()
.Transform(dd => new CountryViewModel(dd))
.Sort(SortExpressionComparer<CountryViewModel>.Ascending(p => p.Info.Name))
.ObserveOnDispatcher()
.Bind(CountryItems)
.Subscribe();
var selector = CountryItems.ToObservableChangeSet()
.ToCollection()
.Select(items => items.FirstOrDefault())
.Where(vm => vm != null)
.Take(1) //Take 1 to run task once only
.Subscribe(item =>
{
CurrentCountry = item;
});
return new CompositeDisposable(handle, selector);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
RolandPheasant
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's a lot of options for selecting the first item. For example you can: