在AutoSuggestBox中选择项目而不是属性值时,它会绑定到属性。
这是我的xaml。
<AutoSuggestBox x:Name="txtSearchBox" ItemsSource="{Binding}"
PlaceholderText="Search in Distributor" Style="{StaticResource AutoSuggestBoxStyle1}"
Margin="10,25,10,0" DisplayMemberPath="{Binding entityName}" TextMemberPath="{Binding entityName}"
BorderBrush="#000000" BorderThickness="2" TextChanged="txtSearchBox_TextChanged"
SuggestionChosen="txtSearchBox_SuggestionChosen">
<AutoSuggestBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding entityName}"
Tag="{Binding entityId}"/>
</DataTemplate>
</AutoSuggestBox.ItemTemplate>
</AutoSuggestBox>
这是背后的代码
private void txtSearchBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
List<Master_User_Hierarchy_VM> lstUserHierarchy = new List<Master_User_Hierarchy_VM>();
txtSearchBox.ItemsSource = null;
foreach (Master_User_Hierarchy_VM obj in lstMaster_UserHierarchy_VM)
{
if (sender.Text != "")
{
if (obj.entityName.Contains(sender.Text))
{
lstUserHierarchy.Add(obj);
}
}
}
txtSearchBox.ItemsSource = lstUserHierarchy;
}
else if (args.Reason == AutoSuggestionBoxTextChangeReason.SuggestionChosen)
{
//txtSearchBox.Text = txtSearchBox.Items[0].ToString();
}
}
private void txtSearchBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
{
txtSearchBox.Text = ((Master_User_Hierarchy_VM)args.SelectedItem).entityName;
}
这是我输入一个角色
点击此列表中的项目时
我再次在建议框中找到所选项目。当我点击它时,我得到属性名称而不是值
答案 0 :(得分:8)
使用
TextMemberPath="entityName"
而不是
TextMemberPath="{Binding entityName}"
答案 1 :(得分:3)
解决方案是我使用了UpdateTextOnSelect =“False”&amp;在Code背后做了明确的绑定。
答案 2 :(得分:0)
将DisplayMemberPath
设置为`DisplayMemberPath =&#34; entityName&#34;,不需要绑定。
答案 3 :(得分:0)
我通过遵循UWP / Xaml示例解决了这个问题。这是我的实施。
<强>型号:强>
public class Partner : EntityBase
{ //Name
public string Name { get; set; }
//Address
public string Street { get; set; }
...
//It is important to return the formated string
public string DisplayName { get { return string.Format("{0}", Name); } }
}
<强> XAML:强> 无需实施SuggestionChosen事件。
<AutoSuggestBox PlaceholderText="Search"
DisplayMemberPath="DisplayName"
TextMemberPath="DisplayName"
QueryIcon="Find"
Width="200"
TextChanged="{x:Bind ViewModel.SupplierAutoSuggest_TextChanged}"
>
<!--If you are using a DataTemplate you need to delete the DisplayMemberPath property, if not you will get an unhandled exception-->
<!--AutoSuggestBox.ItemTemplate>
<DataTemplate x:DataType="models:Partner">
<TextBlock
Text="{Binding Name, Mode=OneWay}"/>
</DataTemplate>
</!-->
</AutoSuggestBox>
TextChanged事件方法(在相应的viewModel类中实现):
public async void SupplierAutoSuggest_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
var queryText = sender.Text;
if(args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
if(!string.IsNullOrEmpty(queryText))
{
//IEnumerable Returned
var suggestion = await App.Repository.Partners.GetAsync(queryText);
if (suggestion.Any())
sender.ItemsSource = suggestion;
}
}
}
请注意,您可以将x:bind用于MVVM模式porpouse