2015-08-13 79 views
0

我有顯示的版本像V14下拉一個簡單的用戶控件,V15等綁定是不是列表框工作,同時使用MVVM

我還有一個列表框,其時的ItemSource綁定到視圖模型,它們取決於財產版本DropDown的SelectedValue。

視圖模型是這樣的:

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的屬性。

問題我面對的是,我運行應用程序,我看到的版本正在填充,但沒有在列表框中。

我將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> 

+1

你可以發佈你的XAML的列表框,以便我們可以看到你的綁定前端如何 – kevchadders

+0

問題可能在於你如何聲明datacontext代碼隱藏。使用MVVM的關鍵是不要在UI元素.cs文件中編寫代碼。您應該在XAML中聲明datacontext和綁定。 – Origence

+2

從'Environments'屬性獲取器返回一個新的'ObservableCollection '是一個非常糟糕的主意。如果屬性被多次調用,您將處理集合的多個不同實例。 – Clemens

回答

1

我相信這是因爲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}" />