2016-11-24 73 views
1

我正在使用MVVM模式將ViewPage中的AutoSuggestBox的屬性綁定到ViewModel。當我在一個Grid或一個stackPanel內時,這工作正常。模板10 UWP如何綁定到MenuFlyoutItem中的autoSuggestBox

但是,一旦我將AutoSuggestBox放在Button的MenuFlyout中。在編譯時出現以下錯誤

錯誤對象引用未設置爲對象的實例。

有關如何綁定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 > 
+0

也MenuFlyout將是第一,然後MenuFlyoutItem。 – mvermef

回答

0

我相信你的錯誤是因爲你正在使用ControlTemplate,它會立即改變頁面中的數據上下文,使你的ViewModel超出範圍。更重要的是ControlTemplates不支持x:Bind。這意味着你不能使用方便的x:綁定到事件,並需要創建命令。您將不得不使用行爲來最簡單地完成此操作。

與此類似的東西。

<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}" /> 

祝你好運。 /傑裏

1
<Button Content="Button" Margin="10,0" > 
    <Button.Flyout> 
     <Flyout Placement="Top"> 
       <AutoSuggestBox ... /> 
     </Flyout> 
    </Button.Flyout> 
    </Button> 

不知道來的需要它是在MenuFlyout性質。爲什麼會讓自己感到非常痛苦,因爲它可能只是在按鈕本身的Flyout子類型中?

至於綁定這與Template10沒有任何關係。這可能與未初始化的集合有關。驗證你綁定的那些集合是否已經被正確創建(例如,new List<yourtype>()例如)

相關問題