2016-11-22 23 views
2

拼写检查器我想禁用了Windows Phone 8.1的应用程序,这是在默认情况下,在一个AutoSuggestBox拼写检查器,但不能期望:如何禁用的AutoSuggestBox

Screenshot of app in emulator

的控制的标记:

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

我怎样才能做到这一点的IsSpellCheckEnabled财产上的内部文本框变为假从标记或代码?

现有的解决方案,我发现无论是处理与其它平台上同样的问题(是这样的:

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删除样式标签,自动提示功能恢复。

回答

2

不幸的是它似乎AutoSuggestBox控制不具有财产IsSpellCheckEnabled所以为了做到这一点,你需要创建一个ControlTemplateTextBox控制,它包含属性IsSpellCheckEnabled并将其设置为那里。

你想要做的第一件事就是在项目中创建一个ResourceDictionary如果你还没有已经有了一个:

resourcedictionary

在这个例子中我已经给我的样式名称的目的.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> 

如果因任何原因,你不能去了XAML在App.xaml然后做<Application搜索,你应该能够这样做。

然后在页面本身,你可以创建一个AutoSuggestBox和参考AutoSuggestBoxStyle

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

在我的例子,我有两个AutoSuggestBoxes

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

这是怎么回事看我的模拟器:

emulator example

正如您所看到的顶部AutoSuggestBox哪些引用样式不显示红色线条,如底部所示。

希望这会有所帮助。

+0

谢谢!我遵循你的指示,并达到我所需要的。但是现在测试失败 - 实际的自动建议功能被打破。当我从目标自动提示框中移除样式属性时,它会再次运行。该模板中必须缺少一些与数据绑定相关的内容。请参阅编辑。 – dlatikay

+0

可能,该模板需要''部分...试图弄清楚它在MSDN文章中是否被截断......是否没有办法指定仅部分一个模板,并从内置继承其余的? – dlatikay

+0

这可能与弹出式元素有关。我直到明天都不在我的Dev机器上,所以不能做任何测试,但是当我回到它并更新我的答案时,如果我设法弄清楚,我会看看。 – Bugs