将ListBos ItemSource绑定到Combobox的selectedVAlue

时间:2015-08-14 11:50:35

标签: wpf xaml

我有一个public class User { private final SetProperty<Certificate> certificates = new SimpleSetProperty<>(FXCollections.observableSet()); public final ObservableSet<Certificate> getCertificates() { return certificatesProperty().get(); } public final void setCertificate(ObservableSet<Certificate> certificates) { certificatesProperty().set(certificates); } public SetProperty<Certificate> certificatesProperty() { return certificates ; } // other properties.... } 被绑定到ViewModel中定义的ComboBox。 我还有一个List<String>,其ItemSource取决于ListBox中的SelectedValue

在ViewModel中,我有以下属性:

  • ComboBox
  • List<String> ComboBoxSourcestring SelectedValue所选值)
  • ComboBox

现在,当ObservableCollection<String> ListBoxSource ComboBox更改时,我在ViewModel(selectedvalue)中明确设置SelectedVAlue属性,这会引发DataContext事件在PropertyChange上,以便更新ListBoxSource

我的问题是如何在没有在ViewModel中明确设置ListBox的情况下执行此操作,即我可以将SelectedVAlue绑定到我的ComboBox.SelectedVAlue属性吗?

这是我的XAML:

SelectedVAlue

以下是Code Behind:

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

1 个答案:

答案 0 :(得分:1)

使用SelectedItem代替SelectedValue,因为它更适合绑定,只需创建一个SelectedVersion属性,将此属性绑定到combobox's SelectedItem,无论何时更改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>