在JavaFX中,如何绑定两个列表视图以使它们的selectedIndex始终相同?

发布于 2024-08-30 01:26:39 字数 55 浏览 4 评论 0原文

我有两个(或更多)并排的 ListView。我需要它们作为一个整体,以便每个的选定索引始终相同。

I have two (or more) ListView's that are side by side. I need them to act as one so the selected index of each is always the same.

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

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

发布评论

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

评论(2

烏雲後面有陽光 2024-09-06 01:26:39

这应该可行:),也许吧。

var lv1 = ListView {
}
var lv2 = ListView {
}

var onSync = false;    

var sel1 = bind lv1.selectedIndex on replace {
    if (not onSync) {
        onSync = true;
        lv2.select(sel1);
        onSync = false;
    }
}
var sel2 = bind lv2.selectedIndex on replace {
    if (not onSync) {
        onSync = true;
        lv1.select(sel2);
        onSync = false;
    }
}

This should work :), maybe.

var lv1 = ListView {
}
var lv2 = ListView {
}

var onSync = false;    

var sel1 = bind lv1.selectedIndex on replace {
    if (not onSync) {
        onSync = true;
        lv2.select(sel1);
        onSync = false;
    }
}
var sel2 = bind lv2.selectedIndex on replace {
    if (not onSync) {
        onSync = true;
        lv1.select(sel2);
        onSync = false;
    }
}
用心笑 2024-09-06 01:26:39

“与逆绑定”似乎是一个选项:

var a;
var b = bind a with inverse;

仅适用于简单表达式。任何更复杂的事情都会生成警告/错误。

但这不是因为 ListView 的 selectedIndex 是公共读取的(感谢更正)。

您必须这样做:

var lv1 = ListView {
}
var lv2 = ListView {
}
var sel1 = bind lv1.selectedIndex on replace {
    lv2.select(sel1);
}
var sel2 = bind lv2.selectedIndex on replace {
    lv1.select(sel1);
}

您可能还想在这里和那里添加一些 if 以避免额外的 select() 调用。

"bind with inverse" seems to be an option:

var a;
var b = bind a with inverse;

works only for simple expressions. anything more complex will generate a warning/error.

Except that it isn't because of ListView's selectedIndex is public-read (thaks for the correction).

You will have to do it like this:

var lv1 = ListView {
}
var lv2 = ListView {
}
var sel1 = bind lv1.selectedIndex on replace {
    lv2.select(sel1);
}
var sel2 = bind lv2.selectedIndex on replace {
    lv1.select(sel1);
}

You also may want to add some ifs here and there to avoid extra select() calls.

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