如何向 WPF 桌面工具添加浮动操作按钮?

How to add a floating action button to WPF Desktop tool?

我正在编写一个数据库工具以用作 Windows 桌面应用程序。我正在使用 C#、WPF 和 SQLite。如何在 ListView 前面添加浮动操作按钮?

至于将按钮放置在另一个元素上,有多种选择,这取决于您的用例,哪种最适合。在此示例中,我将 DockPanel 嵌套在具有两行的 Grid 中。那里的 ListView 跨越两行,而 DockPanel 在第二行。

<Grid>
   <Grid.RowDefinitions>
      <RowDefinition/>
      <RowDefinition Height="Auto"/>
   </Grid.RowDefinitions>
   <ListView Grid.Row="0" Grid.RowSpan="2" ItemsSource="{Binding StringItems}"/>
   <DockPanel Grid.Row="1" LastChildFill="False">
      <Button DockPanel.Dock="Right" Width="50" Height="50" Content="-" Margin="5"/>
      <Button DockPanel.Dock="Right" Width="50" Height="50" Content="+" Margin="5"/>
   </DockPanel>
</Grid>

如果您想要更 Android 的按钮,您可以在浮动操作按钮样式上使用 Material Design WPF library that offers different button styles for that purpose. See the documentation,例如:

<Button DockPanel.Dock="Right" Content="-" Margin="5" Style="{StaticResource MaterialDesignFloatingActionButton}"/>

这是两种变体的结果。