ItemTemplate 不影响 AutoCompleteBox 的选定项目
我正在使用 wpf 工具包 AutoCompleteBox 并且我已经设置了 Item 模板。 问题:弹出列表中的项目看起来很棒,但它对上面的文本框(所选项目)没有生效。
XAML:
<Controls:AutoCompleteBox x:Name="x" ItemFilter="SearchPerson" Margin="20">
<Controls:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<Rectangle Width="10" Height="10" Fill="LightGreen"/>
<TextBlock Text="{Binding Age}" />
</StackPanel>
</DataTemplate>
</Controls:AutoCompleteBox.ItemTemplate>
</Controls:AutoCompleteBox>
隐藏代码:
public partial class MainWindow : Window
{
public List<Person> Persons { get; set; }
public MainWindow() {
InitializeComponent();
Persons = new List<Person> {
new Person{Name = "Jhon",Age=35},
new Person{Name = "Kelly",Age=40}};
x.ItemsSource = Persons;
DataContext = this;
}
bool SearchPerson(string search, object value) {
return (value as Person).Name.ToLower().Contains(search);
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
I'm using wpf toolkit AutoCompleteBox and I've set the Item template.
The problem : the Items in the pop-up list look great but it didn't take effect on the textbox above (the selected item).
XAML:
<Controls:AutoCompleteBox x:Name="x" ItemFilter="SearchPerson" Margin="20">
<Controls:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<Rectangle Width="10" Height="10" Fill="LightGreen"/>
<TextBlock Text="{Binding Age}" />
</StackPanel>
</DataTemplate>
</Controls:AutoCompleteBox.ItemTemplate>
</Controls:AutoCompleteBox>
Code behind:
public partial class MainWindow : Window
{
public List<Person> Persons { get; set; }
public MainWindow() {
InitializeComponent();
Persons = new List<Person> {
new Person{Name = "Jhon",Age=35},
new Person{Name = "Kelly",Age=40}};
x.ItemsSource = Persons;
DataContext = this;
}
bool SearchPerson(string search, object value) {
return (value as Person).Name.ToLower().Contains(search);
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想在
TextBox
中显示什么?要显示所选人员的姓名,您可以像这样添加ValueMemberPath
如果您想在
TextBox 中显示
您可以将Name
和Age
ValueMemberBinding
与转换器一起使用,我们直接绑定到
Person
并在转换器中返回姓名和年龄更新
您'您必须编辑
AutoCompleteBox
的模板。TextBox
无法显示Rectangle
,因此解决方案将取决于您希望如何处理。下面是一个在模板中显示TextBox
、Rectangle
和TextBlock
的示例。它应该给你你想要的What do you want to show in the
TextBox
? To display the Name of the Selected person you may addValueMemberPath
like thisIf you want to display both
Name
andAge
in theTextBox
you can useValueMemberBinding
with a converterWhere we bind directly to the
Person
and return Name and Age in the converterUpdate
You'll have to edit the Template of the
AutoCompleteBox
.TextBox
can't display aRectangle
so the solution will be depending on how you want this to be handled. Here is an example that displays aTextBox
, aRectangle
and aTextBlock
in the Template. It should give you what you want