Mvvm 模板10 UWP如何绑定到菜单项中的autoSuggestBox

Mvvm 模板10 UWP如何绑定到菜单项中的autoSuggestBox,mvvm,uwp,autosuggest,template10,flyout,Mvvm,Uwp,Autosuggest,Template10,Flyout,我正在使用MVVM模式将ViewPage中AutoSuggestBox的属性绑定到我的ViewModel。当我在网格或stackPanel中时,这可以很好地工作 但是有一次我把自动建议框放进了一个菜单里,没有一个按钮。我在编译时遇到以下错误 错误对象引用未设置为对象的实例 关于如何在MenuFlyoutItem内绑定AutoSuggestBox属性的任何指导信息 下面是我试图编译的代码 <Button> <Button.Flyout> <MenuFlyo

我正在使用MVVM模式将ViewPage中AutoSuggestBox的属性绑定到我的ViewModel。当我在网格或stackPanel中时,这可以很好地工作

但是有一次我把自动建议框放进了一个菜单里,没有一个按钮。我在编译时遇到以下错误

错误对象引用未设置为对象的实例

关于如何在MenuFlyoutItem内绑定AutoSuggestBox属性的任何指导信息

下面是我试图编译的代码

<Button>
  <Button.Flyout>
    <MenuFlyoutItem >
          <MenuFlyoutItem.Template>
             <ControlTemplate TargetType="MenuFlyoutItem">
                 <AutoSuggestBox Header="What's your name?"
                  TextChanged="{x:Bind ViewModel.FilterUsuals}"
                  QuerySubmitted="{x:Bind ViewModel.ProcessQuery}"
                  SuggestionChosen="{x:Bind ViewModel.ProcessChoice}"
                  ItemsSource="{Binding Elements}"
                  Text="{x:Bind ViewModel.SearchText, Mode=TwoWay}"
                  QueryIcon="Find" />
            </ControlTemplate>
          </MenuFlyoutItem.Template>
    </MenuFlyoutItem>
</Button.Flyout>
</Button >


不确定是否需要将其置于菜单中。为什么这样做会给你自己带来如此多的痛苦,而它可能只是按钮本身的一个弹出子类型


至于绑定,这与Template10无关。它可能与未初始化的集合有关。验证您要绑定的集合是否已正确创建(例如,
new List()

我认为您的错误是因为您使用的控制模板会立即更改页面中的数据上下文,从而使您的ViewModel超出范围。更重要的是,ControlTemplates中不支持x:Bind。这意味着您不能使用方便的x:Bind来绑定事件,需要创建命令。您必须使用行为来最轻松地完成此任务

类似的东西

<AutoSuggestBox>
   <interactivity:Interaction.Behaviors>
      <core:EventTriggerBehavior EventName="TextChanged">
         <core:InvokeCommandAction Command="{Binding TextChangedCommand}" />
      </core:EventTriggerBehavior>
   </interactivity:Interaction.Behaviors>
</AutoSuggestBox>
public class AutoSuggestBoxAttachedProperties : Windows.UI.Xaml.DependencyObject
{
    public static ICommand GetTextChangedCommand(Windows.UI.Xaml.Controls.AutoSuggestBox obj)
        => (ICommand)obj.GetValue(TextChangedCommandProperty);
    public static void SetTextChangedCommand(Windows.UI.Xaml.Controls.AutoSuggestBox obj, ICommand value)
        => obj.SetValue(TextChangedCommandProperty, value);
    public static readonly DependencyProperty TextChangedCommandProperty =
        DependencyProperty.RegisterAttached("TextChangedCommand", typeof(ICommand),
            typeof(AutoSuggestBoxAttachedProperties), new PropertyMetadata(null, TextChangedCommandChanged));

    public static object GetTextChangedCommandParameter(Windows.UI.Xaml.Controls.AutoSuggestBox obj)
        => (object)obj.GetValue(TextChangedCommandParameterProperty);
    public static void SetTextChangedCommandParameter(Windows.UI.Xaml.Controls.AutoSuggestBox obj, object value)
        => obj.SetValue(TextChangedCommandParameterProperty, value);
    public static readonly DependencyProperty TextChangedCommandParameterProperty =
        DependencyProperty.RegisterAttached("TextChangedCommandParameter", typeof(object),
            typeof(AutoSuggestBoxAttachedProperties), new PropertyMetadata(null));

    private static void TextChangedCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var box = d as Windows.UI.Xaml.Controls.AutoSuggestBox;
        box.TextChanged -= Box_TextChanged;
        if (e.NewValue != null)
        {
            box.TextChanged += Box_TextChanged;
        }
    }

    private static void Box_TextChanged(Windows.UI.Xaml.Controls.AutoSuggestBox sender, Windows.UI.Xaml.Controls.AutoSuggestBoxTextChangedEventArgs args)
    {
        var command = GetTextChangedCommand(sender);
        if (command != null)
        {
            var parameter = GetTextChangedCommandParameter(sender);
            command.Execute(parameter);
        }
    }
}
然后这个

<AutoSuggestBox 
    ex:AutoSuggestBoxAttachedProperties.TextChangedCommand="{Binding TextChangedCommand}" />


祝你好运/Jerry

同样,MenuFlyout将首先成为MenuFlyout,然后是MenuFlyoutItem。
<AutoSuggestBox 
    ex:AutoSuggestBoxAttachedProperties.TextChangedCommand="{Binding TextChangedCommand}" />