gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:26:43 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com