xaml - 使用模板 10 将 AutoSuggestBox 绑定(bind)到 UWP 中的 ViewModel 属性

标签 xaml mvvm uwp template10

在 UWP/Template 10 应用中,我们需要 AutoSuggestBox 来更新 ViewModel 上的 Customer 属性。 AutoSuggestBox 按预期过滤并从客户列表中选择,但 ViewModel 的 Customer 属性保持为空。

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

最后是客户类
public class Customer
{
    public Guid CustomerId { get; set; } = Guid.NewGuid();
    public string FileAs { get; set; }
}

AutoSuggestBox 正在按预期过滤和显示。为什么 MainPageViewModel 的 Customer 属性保持为空?

最佳答案

根据@tao 的建议,我修改了 ViewModel 如下,现在一切都是tickety-boo

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

关于xaml - 使用模板 10 将 AutoSuggestBox 绑定(bind)到 UWP 中的 ViewModel 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40626086/

相关文章:

c# - UWP 应用程序中的自引用泛型类型约束和 XAML

WPF工具包图表: Customize datapoint label

wpf - 在XAML风格中,如何将纯色背景更改为渐变?

c# - 无法获取内部 XAML 绑定(bind)以针对依赖项属性工作

c# - 密码框中的水印

wpf - 当 RenderableSeries 绑定(bind)到 MVVM 时,SciChart 上 RolloverModifier 的 DataTemplate

Windows 10 中的 iOS XPC 模拟

c# - 如何将 DataContext 设置为 self

mvvm - 导航逻辑属于哪里,View、ViewModel 还是其他地方?

c# - UWP MapControl 放大后崩溃