【问题标题】:Selectable controls in WPFWPF 中的可选控件
【发布时间】:2026-02-08 04:50:01
【问题描述】:

WPF 中是否有通用机制允许选择任何或几乎任何控件?例如,您可以在 XAML 中定义的 WrapPanel 上有一些 Image 控件,并且您需要能够一一选择它们,或者通过用鼠标拖过去标记多个。

在代码方面,我可以想象如下:

<WrapPanel> 
    <Image Source="{StaticResource ResourceKey=pic1}" />
    <Image Source="{StaticResource ResourceKey=pic2}" />
    <Image Source="{StaticResource ResourceKey=pic3}" />
    <Image Source="{StaticResource ResourceKey=pic4}" />
    <Image Source="{StaticResource ResourceKey=pic5}" />
</WrapPanel>

【问题讨论】:

    标签: c# wpf xaml controls selectable


    【解决方案1】:

    是的,它叫做ListView

    如果您需要WrapPanel 的外观,只需像这样设置ItemsPanel 属性:

    <ListView>
       <ListView.ItemsPanel>
            <ItemsPanelTemplate>
               <WrapPanel/>
            </ItemsPanelTemplate>
       </ListView.ItemsPanel>
    </ListView>
    

    MSDN 上的文档。

    在您的示例中,您需要将 Image 对象作为 ItemTemplate 的一部分。您可以手动定义 Items 集合或使用 CompositeCollection 作为项目源。

    【讨论】:

      【解决方案2】:

      对我来说,它适用于我在这里找到的解决方案:WrapPanel doesn't wrap in WPF ListView

      它与 BradleyDotNET 的解决方案基本相同,但它在列表视图中包含了 ScrollViewer.Horizo​​ntalScrollBarVisibility="Disabled",通过这样做,它使 wrap 函数无需执行任何其他操作即可工作。

      <ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListView.ItemsPanel>
          <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal" />
          </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        ...
      </ListView>
      

      【讨论】:

        最近更新 更多