有没有选择的WPF列表视图?

nas*_*kew 6 wpf wpf-controls

我正在开发的应用程序有一种由扩展器组成的手风琴,但每个扩展器独立运行,而不是一次只允许一个打开的项目.每个扩展器都与视图模型中集合中的项相关.

我目前的解决方案是使用列表框,然后将列表itemsource绑定到集合,并使项目模板呈现扩展器和扩展器内容.

问题是列表框将每个扩展器视为一个项目(显然),并允许选择和突出显示.突出显示有点难看,可能会被禁用,但选择会导致一些问题,因为它会导致列表滚动显示尽可能多的扩展扩展器.

是否有一个WPF控件有点像stackpanel(可能),它允许我使用项目模板绑定包含的控件但没有选择和突出显示?

在此输入图像描述

            <ListBox Grid.Row="0" Grid.Column="0" Width="Auto" SelectionMode="Single" ItemsSource="{Binding Path=MeasurementSources}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Expander  Header="{Binding Name}" IsEnabled="{Binding Available}">
                        <ListBox Width="Auto" SelectionMode="Single"
                                ItemsSource="{Binding Path=Measurements}"
                                SelectedItem="{Binding Path=SelectedMeasurement}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <WrapPanel>
                                        <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                                        <TextBlock Text=" "/>
                                        <TextBlock Text="{Binding Created}"/>
                                    </WrapPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </Expander>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
Run Code Online (Sandbox Code Playgroud)

flq*_*flq 22

如果你想要没有选择功能的类似List的功能,你应该使用ItemsControl- 顺便说一下它的基类,Selector它的基类是ListBox

然后整个事情就变成了

<ItemsControl Width="Auto" ItemsSource="{Binding Measurements}"
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <WrapPanel>
        <TextBlock Text="{Binding Name}" FontWeight="Bold" />
        <TextBlock Text=" "/>
        <TextBlock Text="{Binding Created}"/>
      </WrapPanel>
    </DataTemplate>
 </ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

显然,在这种情况下,您无法绑定选定的项目.