AutoCompleteBox ...“Expression.Blend.SampleData.Users.CollectionItem”? [英] AutoCompleteBox ... "Expression.Blend.SampleData.Users.CollectionItem"?

查看:50
本文介绍了AutoCompleteBox ...“Expression.Blend.SampleData.Users.CollectionItem”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试AutoCompleteBox控件,但我似乎无法让控件使用我的测试数据。  我进入所有行"Expression.Blend.SampleData.Users.CollectionItem"当我开始输入时,而不是我的数据?

I am testing the AutoCompleteBox control but I can't seem to get the control to use my test data.  I get in all rows "Expression.Blend.SampleData.Users.CollectionItem" instead of my data when I start typing?

推荐答案

默认情况下,自动完成框使用对象上的.ToString()来确定它显示的内容。你看到的是默认的ToString()方法返回对象的类型名称。

如果你控制了数据类型类,你可以覆盖ToString()方法返回你想要的,但是因为你不这样做(如果更改样本数据中的任何内容,Blend会覆盖它),还有其他两个选项。

最简单的方法是这样做:

By default the autocomplete box uses .ToString() on the objects to determine what it displays. What you are seeing is the default ToString() method returning the type name of the object.

If you controlled the data type class, you could override the ToString() method to return what you want, but since you don't (Blend will overwrite it if you change anything in sample data), there are 2 other options.

The simplest is to do this:
<UserControl.Resources>
	<SampleData:SampleDataSource x:Key="SampleDataSource" d:IsDataSource="True"/>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource SampleDataSource}}">
	<input:AutoCompleteBox ItemsSource="{Binding Collection}" ValueMemberPath="Property1">
		<input:AutoCompleteBox.ItemTemplate>
			<DataTemplate>
				<TextBlock Text="{Binding Property1}" />
			</DataTemplate>
		</input:AutoCompleteBox.ItemTemplate>
	</input:AutoCompleteBox>
</Grid>


关键部分是ValueMemberPath,以及显示正确属性的数据模板。

另一种选择是使用ValueMemberBinding,并可以编写自己的IValueConverter来返回你想要的内容。 

The key parts being the ValueMemberPath, and the data template to show the correct property.

The other option is to use ValueMemberBinding, and possible write your own IValueConverter to return what you want. 


这篇关于AutoCompleteBox ...“Expression.Blend.SampleData.Users.CollectionItem”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆