微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – WPF:更改ComboBox的ItemTemplate可以删除在输入时跳过列表的功能.任何方法来解决这个问题?

PersonVM.cs
public class MainWindowVM
{
    public MainWindowVM()
    {
        PersonList = new ObservableCollection<Person>(Employees);
    }

    private Person[] Employees = new Person[]
    {
        new Person { ID = 1,Name = "Adam" },new Person { ID = 2,Name = "Bill" },new Person { ID = 10,Name = "Charlie" },new Person { ID = 15,Name = "Donna" },new Person { ID = 20,Name = "Edward" }
    };

    public ObservableCollection<Person> PersonList { get; set; }
}

Person.cs

public class Person
{
    public string Name { get; set; }
    public int ID { get; set; }
}

MainWindow.xaml(功能工作版本 – 不是我想要显示)

<Window x:Class="TestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <ComboBox Height="23" Width="300"
                  ItemsSource="{Binding Path=Objects}"
                  displayMemberPath="Name"
                  >
        </ComboBox>
    </Grid>
</Window>

MainWindow.xaml(正确显示 – 无法正常工作)

<Window x:Class="TestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <ComboBox Height="23" Width="300"
                  ItemsSource="{Binding Path=Objects}"
                  >
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock DataContext="{Binding}">
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{} {0} | {1}">
                                <Binding Path="ID" />
                                <Binding Path="Name" />
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>
</Window>

第二个代码显示了我想要的组合框显示{ID} | {Name},但它消除了ComboBox一个常用功能.在第一个例子中,当选择ComboBox时,用户可以开始输入它并让它从列表中跳下来.例如,如果按字母A,它跳转到“Adam”,B跳转到“Bill”等.这是ComboBox应该如何运作.但是,当我重写ComboBox ItemTemplate时,它会丢失该功能.是否有另一种方式来绑定我需要的东西,并保持该功能或重新启用? ItemTemplate也许设置错误

解决方法

看到我对这个问题的回答: Can I do Text search with multibinding

不幸的是,TextSearch.Text在DataTemplate中不起作用.我想你有两个选择

选项1.将ComboBox的IsTextSearchEnabled设置为True,覆盖源类中的ToString,并将TextBlock中的MultiBinding更改为绑定

<ComboBox ...
          IsTextSearchEnabled="True">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox> 

public class Person
{
    public override string ToString()
    {
        return String.Format("{0} | {1}",Name,ID);
    }

    public string Name { get; set; }
    public int ID { get; set; }
}

选项2.在源类中创建一个新的Property,您可以将TextSearch.TextPath的Name和ID组合在一起.此外,只要您为Name或ID进行操作,您应该为NameAndId调用OnPropertyChanged

<ComboBox ...
          TextSearch.TextPath="NameAndId"
          IsTextSearchEnabled="True">


public string NameAndId
{
    return String.Format("{0} | {1}",ID);
}

原文地址:https://www.jb51.cc/csharp/93449.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐