I'm able to get items from a database into a combobox, but it's querying way too much data. Also, the selected item isn't displaying the string I expected. Instead, it's showing the object type as a string. I clearly don't understand something and I'm stuck even though I'm following this.
Database table name = CityCountyState, e.g.
A city can be in more than one county, which is why I want to use a complex data template:
Combo box XAML:
Viewmodel:
The problem is obviously at line 27/28, but I don't know how to do it.
Database table name = CityCountyState, e.g.
City County State--------- --------- ---------City1 County1 State1City1 County2 State1City2 County3 State2City3 County4 State3A city can be in more than one county, which is why I want to use a complex data template:
01.<DataTemplate x:Key="ComboBoxCustomTemplate">02. <Grid Margin="0,2,0,2">03. <Grid.ColumnDefinitions>04. <ColumnDefinition />05. <ColumnDefinition />06. </Grid.ColumnDefinitions>07. <Grid.RowDefinitions>08. <RowDefinition />09. <RowDefinition />10. </Grid.RowDefinitions>11. <TextBlock TextAlignment="Left" Grid.ColumnSpan="2" Text="{Binding City}" />12. <StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal">13. <TextBlock Text="County: " />14. <TextBlock Foreground="Blue" Text="{Binding County}" />15. </StackPanel>16. <StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal">17. <TextBlock Text="State: " />18. <TextBlock Foreground="Blue" Text="{Binding State}" />19. </StackPanel>20. </Grid>21.</DataTemplate>Combo box XAML:
1.<telerik:RadComboBox2. IsEditable="True"3. StaysOpenOnEdit="True"4. ItemsSource="{Binding Path=CitiesCountiesStates}"5. SelectedItem="{Binding Path=SelectedCityCountyState}"6. OpenDropDownOnFocus="True"7. IsFilteringEnabled="True"8. ItemTemplate="{StaticResource ComboBoxCustomTemplate}" />Viewmodel:
01.public class AddEditViewModel : ViewModelBase<IDialogView>, IDisposable02.{03. private readonly GeotechLogContext context;04. 05. public ObservableCollection<CityCountyState> CitiesCountiesStates { get; set; }06. private string _SelectedCityCountyState;07. public string SelectedCityCountyState08. {09. get10. {11. return this._SelectedCityCountyState;12. }13. set14. {15. // This doesn't work, but it's what I want to do in concept.16. // Value is always a string that equals the object type17. //18. // CityCountyState x = (CityCountyState) value;19. // this._SelectedCityCountyState = x.City + "," + x.County + "," + x.State;20. this._SelectedCityCountyState = value;21. }22. }23. 24. public AddEditViewModel : base( new AddEditWindow() )25. {26. this.context = new GeotechLogContext();27. this.CitiesCountiesStates = new ObservableCollection<CityCountyState>28. (this.context.CityCountyStates.ToList<CityCountyState>() );29. }30.}The problem is obviously at line 27/28, but I don't know how to do it.