ListView 在 TAB 上插入新项目

发布于 2024-12-09 03:54:45 字数 1284 浏览 0 评论 0原文

拥有一个 WPF ListView,其中的项目绑定到数据对象并由编辑器(文本、日期时间等)表示。我希望能够在用户位于最后一个项目的最后一个编辑器中并按 TAB 时插入新项目。然后将输入焦点设置为新添加项目的第一个编辑器。

到目前为止,我有这个:

private Boolean _tabAddedNewSpec = false;
private void OnBaseEditKeyDown(object sender, KeyEventArgs e)
{
    if (!_tabAddedNewSpec)
    {

        if (e.Key == Key.Tab)
            if (this.listview.SelectedItem == this.listview.Items[this.listview.Items.Count - 1])
                {
                    this.AddSpec();

                    // No further tabbing out of this control, we manage it ourselves in this special case...
                    e.Handled = true;
                    _tabAddedNewSpec = true;

                    // Select last item (is NEW one)
                    this.listview.SelectedItem = this.listview.Items[this.listview.Items.Count - 1];

                }
    }
}
private void OnBaseEditKeyUp(object sender, KeyEventArgs e)
{
    if (_tabAddedNewSpec)
    {
        ((BaseEdit)sender).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        _tabAddedNewSpec = false;
    }
}

这段代码几乎可以达到目的。但是,当存在其他包含验证错误(在业务对象上)的规范时,我不允许添加规范(规范)。问题是,当按 TAB 时,最后一个编辑器上的编辑值尚未传递给业务对象。然后,当调用 this.AddSpec() 时,不会产生任何结果,因为它检测到仍然存在错误。继续关注我...

顺便说一句,这个解决方案对我来说似乎很肮脏。有人有好的建议吗?非常欢迎!

Having a WPF ListView with items bound to a data object and represented by editors (Text, DateTime, etc.). I would like to be able to insert a new item when the users is in the last editor at the last item and presses TAB. Then after set input focus to the first editor of the newly added item.

Thus far I have this:

private Boolean _tabAddedNewSpec = false;
private void OnBaseEditKeyDown(object sender, KeyEventArgs e)
{
    if (!_tabAddedNewSpec)
    {

        if (e.Key == Key.Tab)
            if (this.listview.SelectedItem == this.listview.Items[this.listview.Items.Count - 1])
                {
                    this.AddSpec();

                    // No further tabbing out of this control, we manage it ourselves in this special case...
                    e.Handled = true;
                    _tabAddedNewSpec = true;

                    // Select last item (is NEW one)
                    this.listview.SelectedItem = this.listview.Items[this.listview.Items.Count - 1];

                }
    }
}
private void OnBaseEditKeyUp(object sender, KeyEventArgs e)
{
    if (_tabAddedNewSpec)
    {
        ((BaseEdit)sender).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        _tabAddedNewSpec = false;
    }
}

This code almost does the trick. But, I don't allow that a spec (specification) is added when there are other specs that contain validation errors (on the business object). The problem is that when pressing TAB the editvalue on the last editor isn't yet passed to the business object. Then when calling this.AddSpec() results in nothing because it detects that there are still errors. Follow me still...

And by the way, this solution seems pretty dirty to me. Anybody good advice? Very welcome!

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

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

发布评论

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

评论(1

月下客 2024-12-16 03:54:45

正如之前提到的,该解决方案几乎成功了。首先更新主动控件的绑定,生成了所需的解决方案。使用此代码:

BindingExpression bindingExpression = ((BaseEdit)sender).GetBindingExpression(TextEdit.TextProperty);

if (bindingExpression != null)
  bindingExpression.UpdateSource();

As mentioned before, the solution nearly did the trick. With first updating the binding of the active control the desired solution was produced. Using this code:

BindingExpression bindingExpression = ((BaseEdit)sender).GetBindingExpression(TextEdit.TextProperty);

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