如何禁用AutoSuggestBox上的拼写检查程序

时间:2016-11-22 15:32:43

标签: wpf windows-phone-8.1 windows-store-apps winrt-xaml

我想在Windows Phone 8.1应用程序中禁用拼写检查程序,该应用程序默认在AutoSuggestBox上启用,但不是必需的:

Screenshot of app in emulator

控件的标记:

<AutoSuggestBox 
    Name="txtOrgunit" 
    TextChanged="txtOrgunit_TextChanged"
    SuggestionChosen="txtOrgunit_SuggestionChosen">
</AutoSuggestBox>

如何通过标记或代码实现内部文本框中的IsSpellCheckEnabled属性为false?

我发现现有的解决方案要么在其他平台上处理同样的问题(比如:

How can I disable the spell checker on text inputs on the iPhone

和此:

how to disable spell checker for Android AutoCompleteTextView?

或者他们是笨重的火箭科学,像这样:

https://social.msdn.microsoft.com/Forums/windowsapps/en-US/c2139520-26e9-4a74-819d-defb4e20857c/how-to-disable-spell-check-grammer-in-autosuggestbox?forum=wpdevelop

编辑:在逐字逐句应用第一个答案中提出的解决方案后,OP目标已实现,但控件的功能被破坏(事件被提升,itemssource最终有30个项目,但没有一个显示 - 没有&#34;下拉&#34;再次出现。因此,我在下面给出了txtOrgunit_TextChanged处理程序的源代码:

private void txtOrgunit_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
    if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
    {
        var ui = sender.Text.Trim().ToUpperInvariant();
        var matches = new List<IdAndCaption>();
        var count = 0;
        for (int i = 0; i < Com.MasterdataBasic.Orgunits.Length; ++i)
        {
            var cand = Com.MasterdataBasic.Orgunits[i];
            var cap = String.Format("{0} {1}", cand.Abbrev, cand.LongCap);
            if (cap.ToUpperInvariant().Contains(ui))
            {
                var ele = new IdAndCaption() { Id = cand.OrgID, Caption = cap };
                matches.Add(ele);
                ++count;
                /* UX decided it unreasonable to have the user scroll through more...
                 * should type more letters to restrict further */
                if (count >= 30) break;
            }
        }
        sender.ItemsSource = matches;
        Rec.Report.OrgID = -1;
    }    
}

我验证了当我从autosuggestbox中删除样式标记时,会恢复自动提示功能。

1 个答案:

答案 0 :(得分:2)

不幸的是,AutoSuggestBox控件似乎没有属性IsSpellCheckEnabled所以为了做到这一点,您需要创建一个ControlTemplate控件,TextBox确实包含属性IsSpellCheckEnabled并将其设置为 False

如果您还没有项目,首先要在项目中创建ResourceDictionary

resourcedictionary

出于本示例的目的,我给了他一个名为 Styles.xaml 的名称。

以下代码会为您提供更多或更少的内容。您可能需要调整某些Setter属性,但在您在问题中提供的链接中放弃示例我已经拼凑了这个:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AppNamespace">

<!-- modified default style for AutoSuggestBox -->
<Style x:Name="AutoSuggestBoxStyle" TargetType="AutoSuggestBox">
    <Setter Property="Margin" Value="{ThemeResource TextControlMarginThemeThickness}" />
    <Setter Property="VerticalAlignment" Value="Top" />
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="ListViewItem">
                <Setter Property="Margin" Value="{ThemeResource AutoSuggestListViewItemMargin}" />
                <Setter Property="FontSize" Value="{ThemeResource ContentControlFontSize}" />
                <Setter Property="Foreground" Value="{ThemeResource TextBoxForegroundThemeBrush}" />
            </Style>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="AutoSuggestBox">
                <Grid>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="Orientation">
                            <VisualState x:Name="Landscape"/>
                            <VisualState x:Name="Portrait"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <TextBox x:Name="TextBox"
                         IsSpellCheckEnabled="False"
                         PlaceholderText="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=PlaceholderText}"
                         Header="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Header}"
                         Width="{TemplateBinding Width}"
                         ScrollViewer.BringIntoViewOnFocusChange="False"
                         Canvas.ZIndex="0"
                         Margin="0" />
                    <Popup x:Name="SuggestionsPopup">
                        <Border x:Name="SuggestionsContainer"
                              Background="{ThemeResource AutoSuggestBackgroundThemeBrush}"
                              BorderBrush="{ThemeResource PhoneAccentBrush}"
                              BorderThickness="{ThemeResource TextControlBorderThemeThickness}">
                            <Border.RenderTransform>
                                <TranslateTransform x:Name="UpwardTransform"/>
                            </Border.RenderTransform>
                            <ListView x:Name="SuggestionsList"
                              ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemsSource}"
                              ItemTemplate="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemTemplate}"
                              ItemTemplateSelector="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemTemplateSelector}"
                              ItemContainerStyle="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemContainerStyle}"
                              RenderTransformOrigin=".5,.5">
                                <ListView.RenderTransform>
                                    <ScaleTransform x:Name="ListItemOrderTransform"/>
                                </ListView.RenderTransform>
                            </ListView>
                        </Border>
                    </Popup>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</ResourceDictionary>

将XAML复制到 Styles.xaml

您现在需要引用这个新的ResourceDictionary。这可以在您的App.xaml或页面本身中完成。

App.xaml中,您可以参考:

<Application
    x:Class="App6.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App6">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

</Application>
  

如果由于某种原因您无法访问App.xaml中的XAML,请搜索<Application,您应该能够以这种方式进行搜索。

然后在页面本身中,您可以创建AutoSuggestBox并引用 AutoSuggestBoxStyle

<AutoSuggestBox Style="{StaticResource AutoSuggestBoxStyle}"/>

在我的示例中,我有两个AutoSuggestBoxes

<StackPanel>
    <AutoSuggestBox Style="{StaticResource AutoSuggestBoxStyle}"/>
    <AutoSuggestBox></AutoSuggestBox>
</StackPanel>

这就是我在模拟器中的样子:

emulator example

正如您所看到的那样,引用该样式的顶部AutoSuggestBox没有像底部那样显示红线。

希望这有帮助。