我正在试着做一个访客日志。我有一个ListView,里面有一个访问者列表。
如果您选择一个访问者并单击一个按钮,它将通过更新数据库中指示他们已注销的字段将其从列表中删除,然后刷新列表以仅显示未勾选该字段的访问者。
if (lsvVisitors.SelectedItems.Count > 0)
{
if (MessageBox.Show(string.Join(Environment.NewLine, "Do you wish to sign-out "
+ lsvVisitors.SelectedItems[0].SubItems[0].Text + " "
+ lsvVisitors.SelectedItems[0].SubItems[1].Text),
"Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
== System.Windows.Forms.DialogResult.OK)
{
var entry = _repository.GetLogbookEntryById((LogbookEntry)lsvVisitors.SelectedItems[0].Tag);
_repository.SignOutLogbookEntry(entry);
UpdateList();
}
}
else
{
MessageBox.Show(string.Join(Environment.NewLine,
"You need to select a Visitor to sign-out"),
"Warning",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}我的问题是,当用户在列表中没有人选择的情况下单击signout按钮时,我必须检查selecteditem计数是否>0,如果检查失败,我会告诉用户在列表中选择某个人。现在,如果用户选择了某个人,然后将焦点从列表视图中移除,并且该访问者似乎没有被选中,则所选项目仍然显示为列表中最后一个被选中的人。
我不能在焦点离开框时重置此选项,因为那样他们将无法使用注销按钮注销用户。
我在问,如果列表中的项目不再明显地被选中,我该如何重置列表视图的selecteditems.count?
发布于 2014-02-02 09:15:50
从Ashes999问起,这里有一个例子:zip with solution and demo
它基于我为代码项目写的这篇文章:Selected value, index and more。
我在底部添加了一个textbox,并连接了OnFocus事件以将列表框选定索引设置为-1。这样,您可以更改焦点以将所需对象上的选定索引重置为-1,并将其保留为提交按钮的原样。
如果你想跳过演示(真的很遗憾,但是嘿,谁知道呢…),下面是XAML:
<TextBox Text="Click here to remove index" GotFocus="TextBox_GotFocus" />下面是后面的代码:
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
this.SourceListBox.SelectedIndex = -1;
}(请注意,您需要一个名为SourceListBox的ListBox,或者相应地将其重命名)
https://stackoverflow.com/questions/19968821
复制相似问题