加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – WPF:更改ComboBox的ItemTemplate可以删除在输入时跳过列

发布时间:2020-12-15 03:54:06 所属栏目:百科 来源:网络整理
导读:PersonVM.cs public class MainWindowVM{ public MainWindowVM() { PersonList = new ObservableCollectionPerson(Employees); } private Person[] Employees = new Person[] { new Person { ID = 1,Name = "Adam" },new Person { ID = 2,Name = "Bill" },ne
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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读