Charu
Charu

Reputation: 2717

Binding isn't working for ListBox while using MVVM

I have a simple UserControl with a DropDown showing versions like V14, V15 etc.

I have another ListBox whose ItemSource is binded to a property in the ViewModel which depend upon the SelectedValue of the Version DropDown.

ViewModel Looks like this:

 class MultiSelectEnvironmentContextControlViewModel: ViewModelBase
    {
        private string selectedVersion;
        private DomainFacade domainFacade;
        private ObservableCollection<string> environments= new ObservableCollection<string>(); 

        public MultiSelectEnvironmentContextControlViewModel()
        {
            domainFacade = ((App) Application.Current).DomainFacade;

        }
        public IEnumerable<string> EnvironmentVersions
        {
            get
            {
                return (domainFacade.GetEnvironmentVersions().Select(v => "Version " + v));
            }
        }    

        public string SelectedVersion
        {
            get { return selectedVersion; }
            set
            {
                selectedVersion = value;
                RaisePropertyChanged("Environments");
            }
        }


                 public ObservableCollection<string> Environments
    {
        get
        {
            environments = (ObservableCollection<string>)(domainFacade.GetEnvironments(SelectedVersion));
            return environments;
        }
    }

    }

I am keeping track of the SelectedVersion in a property which raises PropertyChanged on Environments so that whenever SeelectedVersion changes, the Environments should update the UI.

Issue I am facing is, ehn I run the application, I see the versions being populated, but there is nothing in the ListBox.

I am setting the DataContext of the UserControl to the ViewModel in the constructor of the UserControl.

Here is how my Control.cs file looks like:

 public partial class MultiSelectEnvironmentContextControl : UserControl
    {            
        private static MultiSelectEnvironmentContextControlViewModel dataContext = new MultiSelectEnvironmentContextControlViewModel();

        public MultiSelectEnvironmentContextControl()
        {
            InitializeComponent();
            this.DataContext = dataContext;
           dataContext.SelectedVersion = (string)this.ComboBoxVersions.SelectedItem;

        }              

        private void ComboBoxVersions_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
          dataContext.SelectedVersion = ((ComboBox) sender).SelectedValue.ToString();              
        }
}

Here is the XAML:

    <ComboBox Grid.Column="0" Grid.Row="0" x:Name="ComboBoxVersions" SelectedIndex="0" Margin="10" SelectionChanged="ComboBoxVersions_OnSelectionChanged" ItemsSource="{Binding EnvironmentVersions}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

    <StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="0" Margin="10">
        <TextBlock VerticalAlignment="Center" Margin="0,0,10,0">Tests to be run for:</TextBlock>
        <ComboBox  Name="ComboBoxFileTypeSelector" ItemsSource="{Binding AvailableValidationTypes}" DisplayMemberPath="Key" SelectedValuePath="Value" SelectedIndex="0">
        </ComboBox>
    </StackPanel>

    <ListBox x:Name="ListBoxEnvironments" Grid.Column="0" Grid.Row="1" Height="300" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Margin="10" SelectionMode="Multiple" ItemsSource="{Binding Environments}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal" HorizontalAlignment="Left" Width="800" >
                </WrapPanel>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox x:Name="CheckBoxEnvironment" Content="{Binding}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Margin="5">
                </CheckBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

Upvotes: 0

Views: 98

Answers (1)

Liero
Liero

Reputation: 27360

I believe this is because SelectedVersion has values like "Version 1", "Version 2", etc, but your method DomainFacade.GetEnvironments(string version) expects values like "1", "2" etc.

I would write your viewmodel like this:

public class MultiSelectEnvironmentContextControlViewModel : ViewModelBase
{
    private string selectedVersion;
    private DomainFacade domainFacade;
    private IEnumerable<string> environments;

    public MultiSelectEnvironmentContextControlViewModel()
    {
        domainFacade = ((App)Application.Current).DomainFacade;
        EnvironmentVersions = domainFacade.GetEnvironmentVersions();
    }

    public IEnumerable<string> EnvironmentVersions { get; private set; }

    public string SelectedVersion
    {
        get { return selectedVersion; }
        set
        {
            selectedVersion = value;
            RaisePropertyChanged("SelectedVersion");
            Environments = domainFacade.GetEnvironments(SelectedVersion);
        }
    }

    public IEnumerable<string> Environments
    {
        get { return environments; }
        set
        {
            environments = value;
            RaisePropertyChanged("Environments");
        }
    }
}

}

and applied to formatting of environment versions in view:

<ComboBox SelectedItem="{Binding SelectedVersion}"
          ItemsSource="{Binding EnvironmentVersions}"
          ItemStringFormat="Version: {0}" />

Upvotes: 1

Related Questions