wpf - 如何禁用 AutoSuggestBox 上的拼写检查器

标签 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 个项目) ,但没有显示任何一个 - 不再出现“下拉菜单”)。因此,我在下面给出了 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控件没有属性 IsSpellCheckEnabled因此,为了做到这一点,您需要创建一个 ControlTemplateTextBox包含属性 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>

If for whatever reason you can't get to the XAML in your App.xaml then do a search for <Application and you should be able to get to it that way.

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

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

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

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

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

emulator example

如您所见,顶部 AutoSuggestBox对样式的引用不会像底部那样显示红线。

希望这有帮助。

关于wpf - 如何禁用 AutoSuggestBox 上的拼写检查器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40745839/

相关文章:

javascript - WebAuthenticationBroker.authenticateAndContinue 身份验证应用程序未激活后

windows-8 - 如何使用 nuget 分发特定于处理器的 WindowsStore 程序集

.net - Metro 应用程序 - ListView - 如何替换 ListView 项目的背景颜色

WPF 圆角文本框

wpf - OxyPlot 图表中的可绑定(bind)注释

xaml - Windows Phone 8.1 类似 Bing 的幻灯片页面动画

c# - 如何在标签上显示百分比c#

wpf - 如何获取 WPF ListView 中所选项目容器的坐标

wpf - 动态宽度绑定(bind)到自定义组合框中使用的下拉菜单(弹出窗口)

wpf - Windows Phone 8.1 Tab/滑动控制