WPF AutoCompleteBox - 如何限制它仅从建议列表中选择?

发布于 2024-08-28 19:18:08 字数 112 浏览 14 评论 0原文

我想限制 WPF AutoCompleteBox(wpf 工具包控件)仅从建议列表中选择一个项目。它不应该允许用户输入他们想要的任何内容。

有人可以建议我如何实现这个吗?任何示例代码都值得赞赏。

I would like to restrict WPF AutoCompleteBox (wpf toolkit control) to select an item only from the suggestion list. It should not allow users to type whatever they want.

Can someone suggest me how to implement this? Any sample code is appreciated.

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

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

发布评论

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

评论(3

压抑⊿情绪 2024-09-04 19:18:08

我是这样做的。创建派生类并重写 OnPreviewTextInput。将您的集合设置为控件的 ItemsSource 属性,它应该可以正常工作。

public class CurrencySelectorTextBox : AutoCompleteBox
{    
    protected override void OnPreviewTextInput(TextCompositionEventArgs e)
    {            
        var currencies = this.ItemsSource as IEnumerable<string>;
        if (currencies == null)
        {
            return;
        }

        if (!currencies.Any(x => x.StartsWith(this.Text + e.Text, true, CultureInfo.CurrentCulture))
        {
            e.Handled = true;
        }
        else
        {
            base.OnPreviewTextInput(e);
        }            
    }
}

Here's how I did it. Create a derived class and override OnPreviewTextInput. Set your collection to the control's ItemsSource property and it should work nicely.

public class CurrencySelectorTextBox : AutoCompleteBox
{    
    protected override void OnPreviewTextInput(TextCompositionEventArgs e)
    {            
        var currencies = this.ItemsSource as IEnumerable<string>;
        if (currencies == null)
        {
            return;
        }

        if (!currencies.Any(x => x.StartsWith(this.Text + e.Text, true, CultureInfo.CurrentCulture))
        {
            e.Handled = true;
        }
        else
        {
            base.OnPreviewTextInput(e);
        }            
    }
}
泪之魂 2024-09-04 19:18:08

您可以通过“Priview key down”事件来限制用户。我希望它能起作用...

You can restrict user by Priview key down event. I hope it will work...

不羁少年 2024-09-04 19:18:08

如果将其数据绑定到属性,例如,

<sdk:AutoCompleteBox ItemsSource="{Binding Sites, Source={StaticResource VmSchedulel}}" ValueMemberPath="SiteName"
                                             SelectedItem="{Binding Site, Mode=TwoWay}" FilterMode="ContainsOrdinal">
                            <sdk:AutoCompleteBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding SiteName}"/>
                                </DataTemplate>
                            </sdk:AutoCompleteBox.ItemTemplate>
                        </sdk:AutoCompleteBox>

如果输入的某些文本与 ItemsSource 中的任何内容都不匹配,则 SelectedItem 将等于 null。
在属性的 set 方法中,您可以不设置该值,因为它为 null,并且该属性将保留其原始值。

 set
        {
            if (value != null)
            {
                BaseRecord.SiteID = value.ID;
                PropChanged("Site");
            }
        }

If you are databinding it to a property, like this for an example

<sdk:AutoCompleteBox ItemsSource="{Binding Sites, Source={StaticResource VmSchedulel}}" ValueMemberPath="SiteName"
                                             SelectedItem="{Binding Site, Mode=TwoWay}" FilterMode="ContainsOrdinal">
                            <sdk:AutoCompleteBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding SiteName}"/>
                                </DataTemplate>
                            </sdk:AutoCompleteBox.ItemTemplate>
                        </sdk:AutoCompleteBox>

If some text is entered which doesn't match anything in the ItemsSource, the SelectedItem will be equal to null.
In the set method of your property, you can just not set the value because it's null, and the Property will keep it's original value.

 set
        {
            if (value != null)
            {
                BaseRecord.SiteID = value.ID;
                PropChanged("Site");
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文