在自动完成组合框中绑定文本属性

发布于 2024-10-27 15:19:19 字数 1252 浏览 0 评论 0原文

有人可以帮助我解决组合框行为的问题吗?这是我的组合框控件(WPF):

<ComboBox Grid.Row="1" Grid.Column="1" Margin="6,0,6,6" Name="comboBoxRegionTown" IsEditable="True" IsTextSearchEnabled="True"  PreviewKeyUp="comboBoxRegionTown_PreviewKeyUp" IsTextSearchCaseSensitive="False" />

想法是使其自动完成(IsEditable =“True”IsTextSearchEnabled =“True”)。然后我在组合框中输入任何文本,它会显示数据库中的一些结果。

这是comboBoxRegionTown_PreviewKeyUp 事件(C#)的代码:

  private void comboBoxRegionTown_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (!string.IsNullOrEmpty(comboBoxRegionTown.Text))
        {
            comboBoxRegionTown.ItemsSource = _br.GetQuery(x => x.Name.Contains(comboBoxRegionTown.Text) && x.RegionTypeId == (int)RegionType.Town).ToList();
            comboBoxRegionTown.IsDropDownOpen = true;
        }
        else
        {
            comboBoxRegionTown.ItemsSource = null;
        }
    }

所以这对我来说效果很好,但随后我单击组合框中找到的任何项目,它会将我所选对象的类型(在本例中为区域)放入 ComboBox.Text 属性中。当然,我可以重写 Region 对象的 ToString() 方法,并在那里设置其公共属性 Name,并且此解决方案工作正常,但我认为最好的方法是找到如何将所选项目绑定到组合框的 Text 属性中。有什么办法可以做到这一点吗?

我已经尝试过查找 Text="{Binding Path=Name}" 和/或 SelectedItem="{Binding Path=Name}" 但在这些情况下总是得到空文本。请帮忙。

could someone help me to solve an issue with combobox behaviour. Here is my combobox control (WPF):

<ComboBox Grid.Row="1" Grid.Column="1" Margin="6,0,6,6" Name="comboBoxRegionTown" IsEditable="True" IsTextSearchEnabled="True"  PreviewKeyUp="comboBoxRegionTown_PreviewKeyUp" IsTextSearchCaseSensitive="False" />

The idea is to make it autocomplete (IsEditable="True" IsTextSearchEnabled="True"). So then I typу any text into combobox it shows some results from database.

Here is a code of comboBoxRegionTown_PreviewKeyUp event (C#):

  private void comboBoxRegionTown_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (!string.IsNullOrEmpty(comboBoxRegionTown.Text))
        {
            comboBoxRegionTown.ItemsSource = _br.GetQuery(x => x.Name.Contains(comboBoxRegionTown.Text) && x.RegionTypeId == (int)RegionType.Town).ToList();
            comboBoxRegionTown.IsDropDownOpen = true;
        }
        else
        {
            comboBoxRegionTown.ItemsSource = null;
        }
    }

So that works fine for me, but then I click to any found item in combobox it puts into ComboBox.Text property the type of my selected object (in this case - Region). Of course I can override ToString() method for my Region object and set there its public property Name and this solution works fine, but I think the best way is to find how to bind selected item into Text property of my combobox. Is there any way to do this?

I've already tryed to ind Text="{Binding Path=Name}" and/or SelectedItem="{Binding Path=Name}" but in these cases just always get empty Text. Please help.

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

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

发布评论

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

评论(1

白衬杉格子梦 2024-11-03 15:19:19

您需要做的是为 ComboBox 设置 ItemTemplate,但如果您只想显示单个属性,则有一种更简单的方法:在ComboBox 会为您生成正确的模板。

What you need to do is set the ItemTemplate for your ComboBox, but if you just want to display a single property there's an easier way: set DisplayMemberPath="Name" in the ComboBox and it'll generate the correct template for you.

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