需要将焦点保持在 wpf 内容面板中

发布于 2024-08-22 01:46:46 字数 198 浏览 5 评论 0原文

我在运行模式下切换 ListViewItem 的内容模板以启用编辑项目。为此,我显示了一个带有“确定”和“取消”选项的面板,我需要用户在移动到另一个项目之前选择其中任何一个选项。我希望该 Panel 表现得像模态 Dialog。 有什么建议吗?

高级感谢, 达斯

I am switching content template of a ListViewItem at run mode to enable editting the item. For that I am showing a Panel with Ok and Cancel options and I need the user to select any of those option before moving to anotheritem. I want that Panel to behave like a modal Dialog.
Any suggestions?

Advanced Thanks,
Das

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪了杯 2024-08-29 01:46:46

您可以尝试收听 PreviewLostKeyboardFocus 事件并当您不想让焦点离开时,将其标记为已处理。这是一个例子。我们有两列,如果您将焦点放入第一列,则在单击“释放焦点”按钮之前您永远不会离开该列:

XAML

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Focus Sample" Height="300" Width="340">
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <GroupBox Header="Press Release Focus to leave">
      <StackPanel PreviewLostKeyboardFocus="StackPanel_PreviewLostKeyboardFocus">
        <TextBox/>
        <Button Content="Release Focus"
                Click="ReleaseFocusClicked"/>
      </StackPanel>
    </GroupBox>
    <GroupBox Header="Try to switch focus here:"
              Grid.Column="1">
      <TextBox/>
    </GroupBox>
  </Grid>
</Window>

C#

using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
  public partial class Window1 : Window
  {
    private bool _letGo;

    public Window1()
    {
      InitializeComponent();
    }

    private void StackPanel_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
      var uie = (UIElement) sender;
      var newFocusDO = (DependencyObject)e.NewFocus;
      if (!_letGo && !uie.IsAncestorOf(newFocusDO))
      {
        e.Handled = true;
      }
    }

    private void ReleaseFocusClicked(object sender, RoutedEventArgs e)
    {
      _letGo = true;
    }
  }
}

我正在执行一项额外检查确保新的焦点目标是否属于我们的面板。如果我们不这样做,我们永远不会让焦点离开当前聚焦的元素。值得一提的是,这种方法并不能阻止用户点击 UI 中的其他按钮。它只是保持焦点。

希望这有帮助。

干杯,安瓦卡。

You could try listen to PreviewLostKeyboardFocus event and mark it as handled when you don't want to let focus go. Here is an example. We have two columns, and if you put focus into the first column you never go out from it until you click Release Focus button:

XAML

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Focus Sample" Height="300" Width="340">
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <GroupBox Header="Press Release Focus to leave">
      <StackPanel PreviewLostKeyboardFocus="StackPanel_PreviewLostKeyboardFocus">
        <TextBox/>
        <Button Content="Release Focus"
                Click="ReleaseFocusClicked"/>
      </StackPanel>
    </GroupBox>
    <GroupBox Header="Try to switch focus here:"
              Grid.Column="1">
      <TextBox/>
    </GroupBox>
  </Grid>
</Window>

C#

using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
  public partial class Window1 : Window
  {
    private bool _letGo;

    public Window1()
    {
      InitializeComponent();
    }

    private void StackPanel_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
      var uie = (UIElement) sender;
      var newFocusDO = (DependencyObject)e.NewFocus;
      if (!_letGo && !uie.IsAncestorOf(newFocusDO))
      {
        e.Handled = true;
      }
    }

    private void ReleaseFocusClicked(object sender, RoutedEventArgs e)
    {
      _letGo = true;
    }
  }
}

I'm doing one extra check to ensure whether new focus target belongs to our panel. If we don't do this we never let focus leave from the currently focused element. It worth to mention that this approach doesn't keep user from clicking on other buttons in UI. It just holds focus.

Hope this helps.

Cheers, Anvaka.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文