如何在CommandBar中显示SearchBox

时间:2017-05-11 06:46:15

标签: uwp-xaml autosuggest search-box commandbar

我想在AutoSuggestBox中添加CommandBar。但是,以下代码不会使AutoSuggestBox伸展。我尝试设置HorizontalContentAlignment="Stretch"HorizontalAlignment="Stretch",但没有任何影响。我错过了什么?

    <CommandBar Grid.Row="0" HorizontalContentAlignment="Stretch">
        <CommandBar.Content>
            <AutoSuggestBox HorizontalAlignment="Stretch" Margin="8 8 8 4" PlaceholderText="Search podcasts" QueryIcon="Find">
                <Interactivity:Interaction.Behaviors>
                    <Core:EventTriggerBehavior EventName="QuerySubmitted">
                        <Core:InvokeCommandAction                         
                    Command="{Binding SearchCommand}"
                    InputConverter="{StaticResource QueryArgsConverter}" />
                    </Core:EventTriggerBehavior>
                </Interactivity:Interaction.Behaviors>
            </AutoSuggestBox>
        </CommandBar.Content>
    </CommandBar>

error image

1 个答案:

答案 0 :(得分:2)

  

我尝试设置Horizo​​ntalContentAlignment =&#34; Stretch&#34;和Horizo​​ntalAlignment =&#34;拉伸&#34;,但它没有影响。

Horizontal​Alignment设置为stretch表示一个元素被拉伸以填充父元素的整个布局槽。在您的代码段中,AutoSuggestBox位于CommandBar的内容中,如果我们选中了CommandBar的{​​{3}},CommandBar的内容使用了ContentControl default template。实际上,父元素是CommandBar而不是ContentControl。这里AutoSuggestBox的大小似乎取决于其子大小,因此AutoSuggestBox没有空间可以填充。

实际上,内容区域设计为与栏的左侧对齐。详情见Content​Control剖析。所以我不建议你伸展它。您可以考虑使用其他控件作为AutoSuggestBox的容器。如果您确实想要拉伸效果,可以手动计算<CommandBar x:Name="cmdbar"> <CommandBar.Content> <AutoSuggestBox x:Name="autobox" Width="{Binding ElementName=cmdbar, Path=ActualWidth}" HorizontalAlignment="Stretch" PlaceholderText="Search podcasts" QueryIcon="Find" /> </CommandBar.Content> </CommandBar> 的宽度。例如:

<VirtualHost *:4567>
  SSLEngine on
  <Directory "/var/www/html/proso/html">
    AllowOverride All
  </Directory>
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/path/to/site
</VirtualHost>