下拉菜单中的默认文本

发布于 2024-08-12 05:55:06 字数 140 浏览 5 评论 0原文

我正在开发一个 wpf 应用程序,当使用组合框控件时,我将 ItemsSource 分配给它。因此,它显示一个未选择任何项目的项目列表,现在用户可以选择他选择的项目。当用户做出选择后,他无法撤消该选择。我希望他能够获得未选择任何项目的初始状态。我怎样才能做到这一点?

I am working on a wpf application, when using Combo Box control i am assigning ItemsSource to it. So, it displays a list of items with no item selected, now user can select an item of his choice. When user has made a selection he has no option to undo that. I want him to be able to get the initial state where no item is selected. How can i do that?

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

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

发布评论

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

评论(3

春庭雪 2024-08-19 05:55:06

在 Windows 窗体编程中,有多种类似的方法。您可以执行以下任一操作:

1) 将空白或 --None-- 记录添加到作为默认记录的组合框数据源。
2)您可以监视按键事件并说使用 ESC 键将所选索引重置回 -1。
3)如果您的组合框允许输入,您还可以允许他们清除文本,如果文本字段为空,则将选定索引设置回-1,这样它就不会重置为选定值。

很多时候我的用户更喜欢选项 1,但这就是我的用户。

这应该会给你一些选择。

There are multiple ways that would also be similar in windows forms programming. You could do either of the following:

1) Add a blank or --None-- record to your combobox datasource that is the default record.
2) You could monitor the keypress event and say use the ESC key to reset the selected index back to -1.
3) If your combobox allows typing you could also allow them to clear the text and onleave if the text field is blank set the selectedindex back to -1 so that it does not get reset to the selected value.

Alot of the time my users prefer option 1 but that's my users.

That should give you some options.

最美的太阳 2024-08-19 05:55:06

我每次都会在默认空白记录(索引为-1)上重置回 --None-- 记录。如果您正在处理“选择已更改”或 ComboBox 上的某些内容的事件,那么如果您将框指向 -1,则可能会遇到空引用错误,而且这也很简单,因为您必须进行的唯一更改是设置所选索引为(例如)0 而不是 -1。

myComboBox.SelectedIndex = 0; //where 0 is a given default content entry.

I'd go for resetting back to a --None-- record over the default blank record (with an index of -1) every time. If you're handling events for "selection changed" or something on the ComboBox then you risk getting null-reference errors if you point the box at -1, and it's just as easy since the only change you'd have to make is set the selected index to (say) 0 rather than -1.

myComboBox.SelectedIndex = 0; //where 0 is a given default content entry.
倒数 2024-08-19 05:55:06

一般来说,我发现如果需要增加 WPF 应用程序的复杂性,将其添加到数据源比将其添加到 XAML 更稳健。

在您的示例中,我将在我的数据源中修复此问题。 有: ,我会添加以下内容:

public IEnumerable<Person> People { get {...} }

如果我的数据源中

public IEnumerable<Person> PeopleWithNull
{
   get
   {
      return (new List<Person> { null }).Concat(People);
   }
}

Generally speaking, I find that if I need to add complexity to a WPF application, adding it to the data source is more robust than adding it to the XAML.

In your example, I'd fix this in my data source. If I have:

public IEnumerable<Person> People { get {...} }

in my data source, I'd add this:

public IEnumerable<Person> PeopleWithNull
{
   get
   {
      return (new List<Person> { null }).Concat(People);
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文