2016-11-16 76 views
1

在UWP/Template 10應用程序中,我們需要AutoSuggestBox更新ViewModel上的Customer屬性。 AutoSuggestBox按預期過濾和選擇客戶列表,但ViewModel的Customer屬性保留爲空。使用模板將AutoSuggestBox綁定到UWP中的ViewModel屬性10

AutoSuggestBox從數據庫中填充。我已經省略了代碼,因爲它運行良好。

此演示項目名爲Redstone。下面是什麼,我認爲是相關的代碼摘錄

MainPage.xaml

<Page x:Class="Redstone.Views.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Behaviors="using:Template10.Behaviors" 
    xmlns:Core="using:Microsoft.Xaml.Interactions.Core" 
    xmlns:Interactivity="using:Microsoft.Xaml.Interactivity" 
    xmlns:controls="using:Template10.Controls" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:uc="using:Redstone.UserControls" 
    xmlns:local="using:Redstone.Views" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:converters="using:Redstone.Validation" 
    xmlns:behaviors="using:Template10.Behaviors" 
    xmlns:vm="using:Redstone.ViewModels" 
    mc:Ignorable="d"> 

<Page.DataContext> 
    <vm:MainPageViewModel x:Name="ViewModel" /> 
</Page.DataContext> 
     <AutoSuggestBox Name="CustomerAutoSuggestBox" 
      Width="244" 
      Margin="0,5" 
      TextMemberPath="{x:Bind ViewModel.Customer.FileAs, Mode=TwoWay}" 
      PlaceholderText="Customer" 
      QueryIcon="Find" 
      TextChanged="{x:Bind ViewModel.FindCustomer_TextChanged}" 
      SuggestionChosen="{x:Bind ViewModel.FindCustomer_SuggestionChosen}"> 

和相關摘錄MainPageViewModel

public class MainPageViewModel : ViewModelBase 
{ 
    Customer _Customer = default(Customer); 
    public Customer Customer { get { return _Customer; } set { Set(ref _Customer, value); } } 

    public void FindCustomer_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args) 
    { 
     if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput) 
     { 
      sender.ItemsSource = CustomerLookupList.Where(cl => cl.Lookup.IndexOf(sender.Text, StringComparison.CurrentCultureIgnoreCase) > -1).OrderBy(cl => cl.FileAs); 
     } 
    } 
    public void FindCustomer_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args) 
    { 
     CustomerLookup selectedCustomer = args.SelectedItem as CustomerLookup; 
     sender.Text = selectedCustomer.FileAs; 
    } 

最後Customer類

public class Customer 
{ 
    public Guid CustomerId { get; set; } = Guid.NewGuid(); 
    public string FileAs { get; set; } 
} 

的AutoSuggestBox是按預期過濾和顯示。爲什麼MainPageViewModel的Customer屬性保留爲空?

+1

你必須改變'FindCustomer_SuggestionChosen'事件的文本,這樣你就不需要設置'TextMemberPath建議xaml中的屬性。而且我找不到任何地方在你的代碼中改變Customer的價值,所以你可能需要改變'FindCustomer_SuggestionChosen'事件中的'Customer'屬性。 – tao

回答

2

在從@tao我已經修改視圖模型如下,所有現在tickety噓

 public void FindCustomer_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args) 
    { 
     CustomerLookup selectedCustomer = args.SelectedItem as CustomerLookup; 
     this.Customer = CustomersService.GetCustomer(selectedCustomer.CustomerId); 
    } 
相關問題