Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf Expander集合,如何让父视图模型知道当前的扩展器对象?_Wpf_Mvvm_Expander - Fatal编程技术网

Wpf Expander集合,如何让父视图模型知道当前的扩展器对象?

Wpf Expander集合,如何让父视图模型知道当前的扩展器对象?,wpf,mvvm,expander,Wpf,Mvvm,Expander,在包含具有子项集合的项的视图中,我有一个ItemsControl,它承载子项集合。单个项目包含在扩展器中。当展开子项时,我需要父视图模型知道正在对哪个子项进行操作。我能够实现一个事件触发器,它将子对象作为参数传递给父视图模型,然后父视图模型可以设置SelectedChildObject属性。这就是我所需要的,但它的不足之处在于,当多个项目被展开时,用户对一个不在最近展开的项目中的项目进行操作。发生这种情况时,他们正在与之交互的项与SelectedChildObject属性不匹配,因为只有最近展开

在包含具有子项集合的项的视图中,我有一个ItemsControl,它承载子项集合。单个项目包含在扩展器中。当展开子项时,我需要父视图模型知道正在对哪个子项进行操作。我能够实现一个事件触发器,它将子对象作为参数传递给父视图模型,然后父视图模型可以设置SelectedChildObject属性。这就是我所需要的,但它的不足之处在于,当多个项目被展开时,用户对一个不在最近展开的项目中的项目进行操作。发生这种情况时,他们正在与之交互的项与SelectedChildObject属性不匹配,因为只有最近展开的对象才是属性值

我见过一些解决方案,它们使用一个列表框来包含扩展器,然后根据列表框IsSelected设置扩展器IsExpanded,但我不喜欢这种解决方案,因为它一次只允许打开一个扩展器,而且似乎不可能让扩展器拉伸来填充列表框中的所有空间,而且这看起来不太好

有没有一种方法可以让父视图模型知道正在对哪个子对象进行操作

<ItemsControl Grid.Row="0" Grid.Column="0"
                          ItemsSource="{Binding Slots, Mode=TwoWay}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Expander Padding="10">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Expanded">
                        <i:InvokeCommandAction Command="{Binding DataContext.ExpandedCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" CommandParameter="{Binding}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <Expander.Header>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="32" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Image Grid.Row="0" Grid.Column="0"
                                           Height="16" Width="16" Source="/WpfApp1;component/Assets/Images/Warning.ico" />
                        <StackPanel Grid.Row="0" Grid.Column="1"
                                                Orientation="Horizontal">
                            <Label Content="Slot: " />
                            <Label Content="{Binding SlotNumber, Mode=TwoWay, ValidatesOnNotifyDataErrors=False}" />
                        </StackPanel>
                    </Grid>
                </Expander.Header>
                <StackPanel Margin="20">
                    <StackPanel Orientation="Horizontal">
                        <Label Content="Slot Number:" Margin="0 5 2 0" Width="100" />
                        <TextBox Text="{Binding SlotNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" 
                                 Style="{DynamicResource Configuration.Input.TextBox}"
                                 Width="30" />
                    </StackPanel>

                    <StackPanel Orientation="Horizontal">
                        <Label Content="Modules:" Margin="0 5 2 0" Width="100" />
                        <ListBox ItemsSource="{Binding Source={x:Static local:SlotsViewModel.AllowedModules}}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <CheckBox Content="{Binding}" 
                                              Command="{Binding DataContext.AddRemoveAllowedModuleCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
                                              CommandParameter="{Binding}"/>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </StackPanel>
                </StackPanel>
            </Expander>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

您试图在插槽上执行什么类型的操作

比如移除最后一个被操纵的插槽?如果是这样,那么您可能必须编写一个行为或控件来捕获PreviewMouseDown事件,并发送一个命令/事件来通知父视图模型。但这并不理想

您可能希望在删除“选定插槽”之前,以任何方式突出显示该插槽。您还可以尝试删除扩展器的背景并使用列表框。因此,当您单击插槽的背景时,它会选择并高亮显示ListBoxItem。通过这种方式,您可以将SelectedItem绑定到VM