【问题标题】:AutoSuggestBox shows up Property name instead of valueAutoSuggestBox 显示属性名称而不是值
【发布时间】:2015-07-24 14:27:26
【问题描述】:

在 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;

    }

这是我输入字符的时候

当我单击此列表中的项目时

我再次在建议框中获得了选定的项目。当我单击它时,我得到属性名称而不是值

【问题讨论】:

    标签: c# windows-phone windows-phone-8.1 winrt-xaml


    【解决方案1】:

    使用

    TextMemberPath="entityName"
    

    而不是

    TextMemberPath="{Binding entityName}"
    

    【讨论】:

      【解决方案2】:

      解决方案是我使用了 UpdateTextOnSelect="False" 并在 Code behind 中进行了显式绑定。

      【讨论】:

      • 这解决了我在使用 tab 按钮时会出现模型名称的问题
      【解决方案3】:

      我确实通过遵循 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

      【讨论】:

        【解决方案4】:

        DisplayMemberPath 设置为`DisplayMemberPath="entityName",无需绑定。

        【讨论】:

        • 试过还是一样..删除后面代码中的绑定并设置 DisplayMemberpath
        猜你喜欢
        • 2015-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-28
        • 1970-01-01
        • 2021-07-23
        相关资源
        最近更新 更多