2015-08-14 33 views
0

我有一個ComboBox它綁定到在ViewModel中定義的List<String>。 我也有ListBox誰是ItemSource取決於SelectedValueComboBox綁定ListBos ItemSource到Combobox的selectedVAlue

在視圖模型,我具有以下性質:

  • List<String> ComboBoxSource
  • string SelectedValueComboBox選定值)
  • ObservableCollection<String> ListBoxSource

現在,當ComboBoxselectedvalue變化,我在ViewModel中設置SelectedVAlue屬性( DataContext)明確表示正在引發PropertyChange事件ListBoxSource以更新ListBox

我的問題是我怎麼能做到這一點沒有明確設置在視圖模型SelectedVAlue即我能系ComboBox.SelectedVAlueSelectedVAlue財產?

這是我的XAML:

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

      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="Version " /> 
        <TextBlock Text="{Binding}" /> 
       </StackPanel> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
    </ComboBox> 


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

這裏是後面的代碼:

/// <summary> 
    /// Handles the OnSelectionChanged event of the ComboBoxVersions control. 
    /// </summary> 
    /// <param name="sender">The source of the event.</param> 
    /// <param name="e">The <see cref="SelectionChangedEventArgs"/> instance containing the event data.</param> 
    private void ComboBoxVersions_OnSelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     var dataContext = this.DataContext as TestRunnerControlViewModel; 
     dataContext.SelectedVersion = ((ComboBox) sender).SelectedValue.ToString(); 
    } 

回答

1

使用SelectedItem而不是SelectedValue,因爲它是比較合適的綁定,只需創建一個SelectedVersion屬性,綁定此屬性爲combobox'sSelectedItem,只要SelectedItem被更改,在該屬性的setter中更新ListBox itemSource。

INotifyPropertyChanged接口是必要的,讓ListBox知道它們的來源已經改變

private List<String> _environmentVersions; 
    public List<String> EnvironmentVersions 
    { 
     get 
     { 
      return _environmentVersions; 
     } 

     set 
     { 
      if (_environmentVersions == value) 
      { 
       return; 
      } 

      _environmentVersions = value; 
      OnPropertyChanged(); 
     } 
    } 

    private ObservableCollection<String> _environments ; 
    public ObservableCollection<String> Environments 
    { 
     get 
     { 
      return _environments; 
     } 

     set 
     { 
      if (_environments == value) 
      { 
       return; 
      } 

      _environments = value; 
      OnPropertyChanged(); 
     } 
    } 

    private String _selectedVersion ; 
    public String SelectedVersion 
    { 
     get 
     { 
      return _selectedVersion; 
     } 

     set 
     { 
      if (_selectedVersion == value) 
      { 
       return; 
      } 

      _selectedVersion = value; 
      //update your listBox itemSource 
      // ... 
     } 
    } 

,並在XAML:

<ComboBox Grid.Column="0" Grid.Row="0" x:Name="ComboBoxVersions" SelectedIndex="0" Margin="10" SelectedItem="{Binding SelectedVersion}" ItemsSource="{Binding EnvironmentVersions}"> 
     <ComboBox.ItemTemplate> 

      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="Version " /> 
        <TextBlock Text="{Binding}" /> 
       </StackPanel> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
    </ComboBox> 
相關問題