我有一个简单的UserControl,DropDown显示版本,如V14,V15等。
我有另一个ListBox,其ItemSource被绑定到ViewModel中的一个属性,该属性依赖于Version DropDown的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,这样每当SeelectedVersion发生变化时,环境都应该更新UI。
我面临的问题是,我运行应用程序,我看到正在填充的版本,但ListBox中没有任何内容。
我将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>
答案 0 :(得分:1)
我认为这是因为SelectedVersion
具有“版本1”,“版本2”等值,但您的方法DomainFacade.GetEnvironments(string version)
需要“1”,“2”等值。
我会像这样编写你的viewmodel:
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}" />