Silverlight 中的自动完成框文本

发布于 2024-08-29 05:11:37 字数 1031 浏览 6 评论 0原文

我无法让 System.Windows.Controls.Input 中的自动完成框按我的意愿工作。当我开始输入显示过滤列表的下拉部分时,它不显示我要绑定的属性,而是显示类名称。

因此,在下面的示例中,当我输入 my - 而不是显示“我的名字”时,它显示 MyNamespace.Person。但是,当我从自动完成列表中选择该项目时,它会在文本框中显示 FullName 属性。我确信我只是在某个地方缺少一个简单的自动完成框属性,但我看不到它。

示例代码:

public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName
        {
            get { return string.Format("{0} {1}", FirstName, LastName); }
        }
    }

在我后面的 xaml 代码中,我创建了一些 Person 对象并将它们存储在一个列表中,并将该列表绑定到自动完成框

List<Person> people = new List<Person>();
people.Add(new Person { FirstName = "My", LastName = "Name" });
people.Add(new Person { FirstName = "Fernando", LastName = "Torres" });
acbNames.ItemsSource = people;

My xaml:

<my:AutoCompleteBox Name="acbNames" ValueMemberPath="FullName" />

/* 输入“my”后,自动完成显示“MyNamespace.Person”而不是“My”名称”,但从列表中选择项目后显示“我的名字”*/

I'm having trouble getting the autocomplete box in System.Windows.Controls.Input working as I wish. When I start typing the dropdown section that displays the filtered list doesn't show the property that I'm binding to, it shows the class name instead.

So in the example below, when I type in my - instead of showing 'My Name' it shows MyNamespace.Person. However, when I select the item from the autocomplete list, it displays the FullName property in the textbox. I'm sure I'm just missing a simple autocomplete box property somewhere but I can't see it.

Example code:

public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName
        {
            get { return string.Format("{0} {1}", FirstName, LastName); }
        }
    }

In my xaml code behind I create some Person objects and store them in a list and bind that list to an autocomplete box

List<Person> people = new List<Person>();
people.Add(new Person { FirstName = "My", LastName = "Name" });
people.Add(new Person { FirstName = "Fernando", LastName = "Torres" });
acbNames.ItemsSource = people;

My xaml:

<my:AutoCompleteBox Name="acbNames" ValueMemberPath="FullName" />

/* after entering 'my', auto complete displays 'MyNamespace.Person' instead of 'My Name', but displays 'My Name' after selecting the item from the list */

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

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

发布评论

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

评论(3

又爬满兰若 2024-09-05 05:11:37

事实证明,我需要对 AutoCompleteBox 的下拉部分使用 ItemTemplate,因此它的 xaml 现在如下所示:

<my:AutoCompleteBox Name="acbNames" ValueMemberBinding="{Binding FullName}">
            <my:AutoCompleteBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding FullName}"/>
                </DataTemplate>
            </my:AutoCompleteBox.ItemTemplate>
        </my:AutoCompleteBox>

It turns out I need to use an ItemTemplate for the dropdown part of the AutoCompleteBox, so the xaml for it would now be as follows:

<my:AutoCompleteBox Name="acbNames" ValueMemberBinding="{Binding FullName}">
            <my:AutoCompleteBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding FullName}"/>
                </DataTemplate>
            </my:AutoCompleteBox.ItemTemplate>
        </my:AutoCompleteBox>
榆西 2024-09-05 05:11:37

是的,您的问题是因为您没有放置项目模板。
但是,如果您放置项目模板但仍然遇到问题,请阅读 Sandro 所写的内容。

我有同样的问题。我使用控件样式的静态资源解决了这个问题,

这是我使用的样式:

<Style x:Key="autocomplete" TargetType="sdk1:AutoCompleteBox">
    <Setter Property="Margin" Value="5,0,5,0"/>
    <Setter Property="MinWidth" Value="100"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property ="HorizontalAlignment" Value="Right"/>
</Style>

如果我不使用此样式,我的自定义项将无法正确显示,因为我在数据项中配置,而是显示类名称。

分享|编辑

这也适用于我,但仅当我应用工具包中的一些自定义主题样式时。
中的主题时,还有一些其他解决方法

当您使用工具包Best、

debarisi

Yes, your problem was because you didn't put item template.
But if you put item template and still got problem read what Sandro has wroted.

I had same problem. I solved it using a Static resource for the Control Style

This is the style i used:

<Style x:Key="autocomplete" TargetType="sdk1:AutoCompleteBox">
    <Setter Property="Margin" Value="5,0,5,0"/>
    <Setter Property="MinWidth" Value="100"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property ="HorizontalAlignment" Value="Right"/>
</Style>

If I don't use this style my Customs Item are not displayed correctly as I configure in DataItem, instead it show the Class name.

share|edit

This works for me too but only when i applied some custom theme style from toolkit.
There are some other workarounds when you use theme from toolkit

Best,

debarisi

踏雪无痕 2024-09-05 05:11:37

我有同样的问题。我使用控件样式的静态资源解决了这个问题,

这是我使用的样式:

    <Style x:Key="autocomplete" TargetType="sdk1:AutoCompleteBox">
        <Setter Property="Margin" Value="5,0,5,0"/>
        <Setter Property="MinWidth" Value="100"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property ="HorizontalAlignment" Value="Right"/>
    </Style>

如果我不使用此样式,我的自定义项将无法正确显示,因为我在数据项中配置,而是显示类名称。

I had same problem. I solved it using a Static resource for the Control Style

This is the style i used:

    <Style x:Key="autocomplete" TargetType="sdk1:AutoCompleteBox">
        <Setter Property="Margin" Value="5,0,5,0"/>
        <Setter Property="MinWidth" Value="100"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property ="HorizontalAlignment" Value="Right"/>
    </Style>

If I don't use this style my Customs Item are not displayed correctly as I configure in DataItem, instead it show the Class name.

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