使用MVVM时,ListBox的绑定不起作用

本文关键字:绑定 不起作用 ListBox MVVM 使用 | 更新日期: 2023-09-27 18:07:54

我有一个简单的UserControl与下拉菜单显示版本,如V14, V15等

我有另一个ListBox,其ItemSource被绑定到ViewModel的属性,该属性依赖于Version下拉菜单的SelectedValue。

ViewModel看起来像这样:

 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;
        }
    }
    }

我在一个属性中跟踪SelectedVersion,该属性在环境上引发PropertyChanged,以便每当SelectedVersion发生变化时,环境应该更新UI。

我面临的问题是,当我运行应用程序时,我看到版本正在填充,但列表框中没有任何内容。

我将UserControl的DataContext设置为UserControl的构造函数中的ViewModel。

下面是我的Control.cs文件的样子:
 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();              
        }
}
这里是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>

使用MVVM时,ListBox的绑定不起作用

我相信这是因为SelectedVersion具有"版本1","版本2"等值,但您的方法DomainFacade.GetEnvironments(string version)期望值如"1","2"等

我会这样写你的视图模型:

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");
        }
    }
}

}

并应用于视图中环境版本的格式化:

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