将AutoSuggestBox添加到Hamburger Navigation的正确方法?

时间:2019-01-17 21:25:09

标签: c# uwp uwp-xaml uwp-navigation

我才刚刚开始学习UWP和xaml。将AutoSuggestBox添加到“侧面导航”面板的正确方法是什么? (对不起,如果代码格式化不好,复制和粘贴的效果不好)

我的Main.xaml具有一个我设置为“可见”的AutoSuggestArea

</VisualStateGroup>
  <VisualStateGroup x:Name="AutoSuggestGroup">
    <VisualState x:Name="AutoSuggestBoxVisible"/>
    <VisualState x:Name="AutoSuggestBoxCollapsed">
  <VisualState.Setters>
    <Setter Target="AutoSuggestArea.Visibility" Value="Visible"/>
  </VisualState.Setters>
</VisualState>
</VisualStateGroup>

,并且在AutoSuggestArea的网格中,我定义了一个AutoSuggestBox

<Grid x:Name="AutoSuggestArea" Height="44" Grid.Row="3" VerticalAlignment="Center">

<ContentControl x:Name="PaneAutoSuggestBoxPresenter" Content="{TemplateBinding AutoSuggestBox}" HorizontalContentAlignment="Stretch" IsTabStop="False" Margin="16,0,16,0" VerticalContentAlignment="Center"/>

<Button x:Name="PaneAutoSuggestButton" Content="&#xE11A;" MinHeight="44" Style="{TemplateBinding PaneToggleButtonStyle}" Visibility="Collapsed" Width="{TemplateBinding CompactPaneLength}"/>

    <AutoSuggestBox Width="234" VerticalAlignment="Center" 
       HorizontalAlignment="Center"
       PlaceholderText="Search" Name ="boxS"                            
       QuerySubmitted="AutoSuggestBox_QuerySubmitted" 
       TextChanged="AutoSuggestBox_TextChanged">
        <AutoSuggestBox.TextBoxStyle>
          <Style TargetType="TextBox">
          <Setter Property="IsHandwritingViewEnabled" Value="False"/>
          <Setter Property="BorderThickness" Value="0"/>
        </Style>
      </AutoSuggestBox.TextBoxStyle>
        <AutoSuggestBox.QueryIcon>
          <SymbolIcon Symbol="Find" Foreground="Black">                                                   
             <SymbolIcon.RenderTransform>
             <CompositeTransform ScaleX="1" ScaleY="1"/>                                         
             </SymbolIcon.RenderTransform>
          </SymbolIcon>
       </AutoSuggestBox.QueryIcon>
    </AutoSuggestBox>
</Grid>

我想要的基本上是与Windows上的Groove Music应用程序相同的行为,其中随着Nav View的关闭或最小化,搜索栏消失了。

相反,我明白了

1 个答案:

答案 0 :(得分:4)

我假设您是用NavigationView来指NavigationPanel

这不是将AutoSuggestBox放入NavigationView的方式。 NavigationView具有NavigationView.AutoSuggestBox属性。您只需在此属性上设置AutoSuggestBox,一切都会按预期进行。像这样:

<NavigationView>
    <NavigationView.AutoSuggestBox>
        <AutoSuggestBox x:Name="NavViewSearchBox" QueryIcon="Find"/>
    </NavigationView.AutoSuggestBox>
</NavigationVew>

您不必自己隐藏/显示此AutoSuggestBoxNavigationView将自动为您执行此操作。另外,您不必将AutoSuggestBox放在任何网格或任何其他物体内。