WPF 工具包 AutoCompleteBox 下拉列表不出现
在我的 WPF 应用程序中,我有一个 UserControl,其中有两个 AutoCompleteBox 控件。此用户控件可以在页面上多次出现。问题是,在 AutoCompleteBox 中输入内容时,不会出现选项下拉列表。我正在处理 Populate 事件,如果我在其中放置一个断点并单步执行,我可以清楚地看到 ItemsSource 中包含项目,因此看起来它正在工作,只是我实际上看不到下拉列表菜单。我按照 http://msdn 中的代码示例进行操作.microsoft.com/en-us/library/dd795156%28v=VS.95%29.aspx。我在这里缺少什么?
XAML:
<my:AutoCompleteBox Name="acboxCoauthorName" Width="175" Unloaded="Control_Unloaded" MinimumPopulateDelay="100" Populating="acboxCoauthorName_Populating">
<my:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=LastOrCompanyName}"/>
</DataTemplate>
</my:AutoCompleteBox.ItemTemplate>
</my:AutoCompleteBox>
C#:
private void acboxCoauthorName_Populating(object sender, PopulatingEventArgs e)
{
e.Cancel = true;
var query = from a in _context.Authors
where a.Display_Name.StartsWith(acboxCoauthorName.Text)
select a;
acboxCoauthorName.ItemsSource = ((ObjectQuery) query).Execute(MergeOption.OverwriteChanges);
acboxCoauthorName.PopulateComplete();
}
当我们这样做时,我似乎也无法对 datacontext 中的对象进行数据绑定。我尝试了上面的数据绑定方法,或者使用了更简单的方法:
这些都不起作用。有什么想法吗?
编辑:别介意第二部分;我将错误的对象设置为控件的 DataContext。
谢谢。
In my WPF application, I have a UserControl that has two AutoCompleteBox controls in it. This UserControl can appear multiple times on a page. The problem is that when typing in AutoCompleteBox, the dropdown of choices doesn't appear. I'm handling the Populating event, and if I put a break point in there and step through, I can clearly see that the ItemsSource contains items in it, so it looks like it's working, except that I don't actually see the dropdown menu. I followed the code sample at http://msdn.microsoft.com/en-us/library/dd795156%28v=VS.95%29.aspx. What am I missing here?
XAML:
<my:AutoCompleteBox Name="acboxCoauthorName" Width="175" Unloaded="Control_Unloaded" MinimumPopulateDelay="100" Populating="acboxCoauthorName_Populating">
<my:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=LastOrCompanyName}"/>
</DataTemplate>
</my:AutoCompleteBox.ItemTemplate>
</my:AutoCompleteBox>
C#:
private void acboxCoauthorName_Populating(object sender, PopulatingEventArgs e)
{
e.Cancel = true;
var query = from a in _context.Authors
where a.Display_Name.StartsWith(acboxCoauthorName.Text)
select a;
acboxCoauthorName.ItemsSource = ((ObjectQuery) query).Execute(MergeOption.OverwriteChanges);
acboxCoauthorName.PopulateComplete();
}
While we're at it, I can't seem to databind the object that's in datacontext, either. I tried the databinding method above, or I went with the simpler:
<my:AutoCompleteBox Name="acboxCoauthorName" Width="175" MinimumPopulateDelay="100" Populating="acboxCoauthorName_Populating" Text="{Binding Path=LastOrCompanyName}">
Neither of those worked. Any ideas?
EDIT: Never mind on the second part; I had set the wrong object to be the control's DataContext.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在 ItemSource 中看到内容,应该没问题...
尝试将 MaximumPrefixLength 设置为 0,以便即使未进行任何过滤也会显示弹出窗口,以便您看到列表包含的内容。还要确保设置过滤器,以便知道您正在使用什么过滤器。
If you are seeing things in the ItemSource, it should be okay...
Try setting the MinimumPrefixLength to 0 so that the popup shows even if no filtering is done so that you see what the list contains. Also make sure you set the Filter so you know what filter you're using.
我对 WPF 很陌生,也遇到了同样的问题。
我还按照微软的模板添加了这个控件。
搜索了大约一个小时后,一个silverlight论坛(http://forums.silverlight.net/ t/178152.aspx/1)有建议:
对我有用,也许他们在编写原始示例后更改了默认值,或者也许对 WPF 和这个特定控件有更多了解的人可以进一步澄清(我很高兴停止拉动我的头发都掉了)。
I'm pretty new to WPF and was having the same issue.
I was also following Microsoft's template for adding this control.
About an hour of searching later, a silverlight forum(http://forums.silverlight.net/t/178152.aspx/1) had the suggestion :
Worked for me, maybe they changed the default value since the original example was written, or maybe someone who knows more about WPF and this particular control can clarify further (I was just glad to stop pulling my hair out).