如果其样式内的文本框的AcceptsReturn为True,则AutoSuggestBox的行为将有所不同

我有一个简单的AutoSuggestBox,其样式通过更改其中的TextBox的几个属性进行了修改。 我在acceptsreturnMinHeight

处修改了属性

这是样式的修改部分

<Style TargetType="AutoSuggestBox">
  <!-- -->
    <TextBox x:Name="TextBox" Style="{TemplateBinding TextBoxStyle}" PlaceholderText="{TemplateBinding PlaceholderText}" Header="{TemplateBinding Header}" Width="{TemplateBinding Width}" acceptsreturn="True" ScrollViewer.BringIntoViewOnFocusChange="False" Canvas.ZIndex="0" MinHeight="20" Margin="0" DesiredCandidateWindowAlignment="BottomEdge" UseSystemFocusVisuals="{TemplateBinding UseSystemFocusVisuals}" />

  <!-- -->
</Style>

这是我完整的示例代码

<Page
    x:Class="TestApplication.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Page.Resources>
        <Style TargetType="AutoSuggestBox">
            <Setter Property="VerticalAlignment" Value="Top" />
            <Setter Property="IsTabStop" Value="False" />
            <Setter Property="TextBoxStyle" Value="{StaticResource AutoSuggestBoxTextBoxStyle}" />
            <Setter Property="UseSystemFocusVisuals" Value="{ThemeResource IsApplicationFocusVisualKindReveal}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="AutoSuggestBox">
                        <Grid x:Name="LayoutRoot">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="Orientation">
                                    <VisualState x:Name="Landscape" />
                                    <VisualState x:Name="Portrait" />
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <TextBox x:Name="TextBox" Style="{TemplateBinding TextBoxStyle}" PlaceholderText="{TemplateBinding PlaceholderText}" Header="{TemplateBinding Header}" Width="{TemplateBinding Width}" acceptsreturn="True" ScrollViewer.BringIntoViewOnFocusChange="False" Canvas.ZIndex="0" MinHeight="20" Margin="0" DesiredCandidateWindowAlignment="BottomEdge" UseSystemFocusVisuals="{TemplateBinding UseSystemFocusVisuals}" />
                            <Popup x:Name="SuggestionsPopup">
                                <Border x:Name="SuggestionsContainer">
                                    <ListView x:Name="SuggestionsList" Background="{ThemeResource AutoSuggestBoxSuggestionsListBackground}" BorderThickness="{ThemeResource AutoSuggestListBorderThemeThickness}" BorderBrush="{ThemeResource AutoSuggestBoxSuggestionsListBorderBrush}" DisplayMemberPath="{TemplateBinding DisplayMemberPath}" IsItemClickEnabled="True" ItemTemplate="{TemplateBinding ItemTemplate}" ItemTemplateSelector="{TemplateBinding ItemTemplateSelector}" ItemContainerStyle="{TemplateBinding ItemContainerStyle}" MaxHeight="{ThemeResource AutoSuggestListMaxHeight}" Margin="{ThemeResource AutoSuggestListMargin}" Padding="{ThemeResource AutoSuggestListPadding}" />
                                </Border>
                            </Popup>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Page.Resources>   
    <StackPanel>
        <AutoSuggestBox x:Name="MyAutoSuggestBox"  Margin="100" Text="{x:Bind Text,Mode=OneWay}" Width="200"/>
    </StackPanel>
</Page>

Here's Sample GiF of What happens

wangxd925 回答:如果其样式内的文本框的AcceptsReturn为True,则AutoSuggestBox的行为将有所不同

在此document中,它提到我们通常将 AcceptsReturn TextWrapping 属性设置为一个多行文本框。因此,您可以添加如下所示的TextWrapping属性。

如果不想添加TextWrapping属性,则可以在Page.Loaded事件中将值设置为Text。加载AutoSuggestBox并将其分配给Text属性后,它将运行良好。

<Style TargetType="AutoSuggestBox">
  <!-- -->
    <TextBox x:Name="TextBox" TextWrapping="Wrap" Style="{TemplateBinding TextBoxStyle}" PlaceholderText="{TemplateBinding PlaceholderText}" Header="{TemplateBinding Header}" Width="{TemplateBinding Width}" AcceptsReturn="True" MinHeight="20" ScrollViewer.BringIntoViewOnFocusChange="False" Canvas.ZIndex="0" Margin="0" DesiredCandidateWindowAlignment="BottomEdge" UseSystemFocusVisuals="{TemplateBinding UseSystemFocusVisuals}" />

  <!-- -->
</Style>
本文链接:https://www.f2er.com/3110471.html

大家都在问