【问题标题】:Autocomplete causes text to scroll to right自动完成使文本向右滚动
【发布时间】:2016-10-17 19:50:02
【问题描述】:

我的可编辑 combobox 上的自动完成功能适用于短项目。

但是,如果所选字符串太宽且不适合 combobox,它会水平向右滚动,可能是由于自动完成功能选择了字符串的其余部分。

这会隐藏用户输入点的当前位置。

如何让它保持插入符号的位置对用户可见?

<ComboBox x:Name="comboBoxCustomer"
    ItemsSource="{Binding Source={StaticResource customerViewSource}}"
    TextSearch.TextPath="CustomerDisplay"
    SelectedValue="{Binding CustomerID, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
    SelectedValuePath="ID"  
    SelectionChanged="comboBoxCustomer_SelectionChanged"
    StaysOpenOnEdit="True"
    IsEditable="True" >
   <ComboBox.ItemTemplate>
      <DataTemplate>
          <TextBlock Text="{Binding CustomerDisplay}" Foreground="{Binding ActiveColour}" />
      </DataTemplate>
  </ComboBox.ItemTemplate>
</ComboBox>

【问题讨论】:

    标签: c# wpf combobox autocomplete


    【解决方案1】:

    你应该试试这个。

    <ComboBox x:Name="comboBoxCustomer"
        ItemsSource="{Binding Source={StaticResource customerViewSource}}"
        TextSearch.TextPath="CustomerDisplay"
        SelectedValue="{Binding CustomerID, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
        SelectedValuePath="ID"  
        SelectionChanged="comboBoxCustomer_SelectionChanged"
        StaysOpenOnEdit="True"
        IsEditable="True" >
       <ComboBox.ItemTemplate>
          <DataTemplate>
              <TextBlock Text="{Binding CustomerDisplay}" Foreground="{Binding ActiveColour}" **TextAlignment="Left"** />
          </DataTemplate>
      </ComboBox.ItemTemplate>
    </ComboBox>
    

    【讨论】:

    • 我已经尝试过TextAlignment 没有任何区别。
    【解决方案2】:

    对textbox 的combobox 使用ScrollToHome 方法。

       private void comboBoxCustomer_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = (TextBox)comboBoxCustomer.Template.FindName("PART_EditableTextBox", comboBoxCustomer);
            tb.ScrollToHome();
        }
    

    【讨论】:

    • 感谢您添加答案。
    • 这对我有用,但是文本是否会为您“弹跳”,或者您是否想办法阻止文本来回“弹跳”。
    • @adondero 它确实会短暂弹跳,但我几乎没有注意到它,它不会干扰打字。我对此很好。你有更好的解决方案吗?
    • @EMett 不是在这个时候,我在花时间试图弄清楚之前就问过了。我会试着回到它,看看我能不能弄清楚什么。
    猜你喜欢
    • 1970-01-01
    • 2014-05-16
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多