1

I have a ListCollectionView which holds a bunch of Items of object Scene. One of the properties in Scene is Location.

When I navigate through the ListCollectionView I want to set the value of the Location property as SelectedItem in a comboBox in the View. Each time I go to a different item in the ListCollectionView I want to show the new location as SelectedItem in the comboBox.

I know how to make this work in a regular TextBox and TextBlock, but not in a ComboBox.

ViewModel

public ListCollectionView SceneCollectionView { get; set; }
private Scene CurrentScene
{
    get { return SceneCollectionView.CurrentItem as Scene; }
    set
    {
        SceneCollectionView.MoveCurrentTo(value);
        RaisePropertyChanged();
    }
}

View

<ComboBox  SelectedItem="{Binding SceneCollectionView/Location, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding AllLocations}}"/>

For textboxes following works perfectly, but not for comboBoxes

 <TextBox Text="{Binding SceneCollectionView/Location, UpdateSourceTrigger=PropertyChanged}"/>

Any Idea how I can get the same behavior in for SelectedItem in ComboBox. I'm fairly new to coding in c#

2
  • What is the type of Location property? Aug 22, 2015 at 8:52
  • It's a string property
    – Phil
    Aug 22, 2015 at 8:54

1 Answer 1

0

If all Locations defined in the supplied collection for your ListCollectionView exist in the Locations defined in your AllLocations property, then your code should work.

For example the following code along with the ComboBox you currently defined in your Xaml works as you expect:

Xaml:

<Grid>
    <ComboBox SelectedItem="{Binding SceneCollectionView/Location, UpdateSourceTrigger=PropertyChanged}" 
              ItemsSource="{Binding AllLocations}"/>
    <TextBox Text="{Binding SceneCollectionView/Location, UpdateSourceTrigger=PropertyChanged}"/>
    <Button  Content="SelectNext" Click="Button_Click"/>
</Grid>

Code:

    public ListCollectionView SceneCollectionView { get; set; }
    public List<string> AllLocations { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;

        var scenes = new List<Scene>();
        scenes.Add(new Scene { Location = "location1"});
        scenes.Add(new Scene { Location = "location2"});
        scenes.Add(new Scene { Location = "location3" });
        SceneCollectionView = new ListCollectionView(scenes);

        AllLocations = new List<string> { "location1", "location2", "location3" };
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        SceneCollectionView.MoveCurrentToNext();
    }

In the above code, when you click the Button, Both ComboBox.SelectedItem and TextBox.Text changes to the next Item.Location defined in your SceneCollectionView.

1
  • This isn't utilizing MVVM Aug 22, 2015 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.