将一些复选框绑定到组合框到 listView

Binding some checkbox into combobox to a listView

我有一个显示在组合框中的机构 collection。其中一个属性是 "IsSelected",它允许我在一个组合框中 select 多个项目。

        <ComboBox Name="CmbEtabTout" 
              ItemsSource="{Binding EtablissementsUtilisateur}"
              Grid.IsSharedSizeScope="True"
              Grid.Column="2" 
              Grid.ColumnSpan="3" 
              Grid.Row="2" 
              Height="25" 
              Width="250">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="30" />
                        <ColumnDefinition SharedSizeGroup="AgentA" Width="auto" />
                        <ColumnDefinition Width="5" />
                        <ColumnDefinition SharedSizeGroup="AgentB" Width="auto" />
                    </Grid.ColumnDefinitions>
                    <CheckBox IsChecked="{Binding IsSelected}" Grid.Column="0"/>
                    <TextBlock Text="{Binding IdEtablissement}" Grid.Column="1"/>
                    <TextBlock Text="{Binding Nom}" Grid.Column="3"/>
                </Grid>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

    <ListView x:Name="LVAgent"
    ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
    ItemsSource="{Binding Agents}" Grid.ColumnSpan="2" Margin="150,0,42,0" Grid.Column="2" Grid.Row="4" Grid.RowSpan="5" >
        <ListView.View>
            <GridView>
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding IsSelected}" 
                                              Command="{Binding DataContext.SelectAgentCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"
                                              CommandParameter="{Binding}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                    <CheckBox IsChecked="{Binding SelectAllAgents}"
                        IsEnabled="True"/>
                </GridViewColumn>
                <GridViewColumn Header="Matricule"
                    Width="110" 
                    DisplayMemberBinding="{Binding Matricule}"/>
                <GridViewColumn Header="Nom" 
                    Width="120"
                    DisplayMemberBinding="{Binding Nom}"/>
                <GridViewColumn Header="Prénom" 
                    Width="120" 
                    DisplayMemberBinding="{Binding Prenom}"/>

            </GridView>
        </ListView.View>
    </ListView>

我的组合框 collection:

        private ObservableCollection<Etablissement> _EtablissementsUtilisateur;
    public ObservableCollection<Etablissement> EtablissementsUtilisateur
    {
        get
        {
            return _EtablissementsUtilisateur;
        }
        set
        {
            if (value != _EtablissementsUtilisateur)
            {
                _EtablissementsUtilisateur = value;
                RaisePropertyChanged(nameof(EtablissementsUtilisateur));
            }
        }
    }

我想知道如何绑定这些组合框来刷新列表:如果我选择三个机构,列表会显示这三个机构的代理商。也许有命令?

     <CheckBox IsChecked="{Binding IsSelected}" Grid.Column="0"/>

看起来像这样:

因为我已经用 "IsSelected" 绑定了我的复选框(用于 SelectAll),我不知道如何绑定刷新后面的 Agent 列表,当我检查时,而不必按下像这样的按钮"validate".

编辑:我现在的问题是,如果我想做这样的事情,例如:

       <CheckBox IsChecked="{Binding IsSelected}" Command="{Binding }" Grid.Column="0" />

我只能绑定到 Etablissement Class 而不能绑定到 ViewModel。 (因为我认为组合框的 itemSource)

目标是,"when any checkbox is checked or unchecked, if I choose three establishments, the list displays the agents of these three establishments"。

"when any checkbox is checked or unchecked" => 属性 更改了事件处理程序

"if I choose three establishments" => if 语句

"the list displays the agents of these three establishments" => 方法调用

假设 Etablissement:INotifyPropertyChanged,我们可以将事件处理程序添加到每个 Etablissement.PropertyChanged。另一种选择是将处理程序添加到 CheckBox.Checked 和 CheckBox.Unchecked.

好处:事件处理程序可以是异步的,所以如果 "method call" 是异步的,你就在家里等待它,这意味着你的 UI 保持响应并且不会锁定。

我会在 UserControl 或 ComboBox 的 Loaded 事件中添加处理程序

Loaded += delegate
{
    PropertyChangedEventHandler propertyChanged = delegate
    {
        //if number of checked items != 3
            //return;
        //update agents
    };
    foreach (var etablissement in EtablissementsUtilisateur)
        etablissement.PropertyChanged += propertyChanged;
}