Search code examples
c#wpfscrollcomboboxtextbox

combobox text input with scrolling


editable wpf combobox. There is a problem that the Textbox does not scroll when entering long strings. like this link Editable combobox text scrolling

TextBox.ScrollToHome () but it does not work.

this is my code and my sample image

<Style TargetType="{x:Type ComboBox}" x:Key="autoComplateComboBox">
            <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
            <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="True"/>
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
            <Setter Property="UIElement.SnapsToDevicePixels" Value="True"/>
            <Setter Property="FrameworkElement.OverridesDefaultStyle" Value="True"/>
            <Setter Property="TextElement.Foreground" Value="Black"/>                
            <Setter Property="FrameworkElement.FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel IsItemsHost="True" 
                                        VirtualizingStackPanel.IsVirtualizing="True"
                                        VirtualizingStackPanel.VirtualizationMode="Recycling" 
                                        KeyboardNavigation.DirectionalNavigation="Contained"/>
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBox">
                        <Grid>
                            <ToggleButton Name="ToggleButton" Grid.Column="2"
                                      ClickMode="Press" Focusable="False" Click="ToggleButton_Click"
                                      IsChecked="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" Template="{StaticResource ComboBoxToggleButtonTemplate}">
                            </ToggleButton>
                            <ContentPresenter Name="ContentSite" Margin="5, 3, 23, 3" IsHitTestVisible="False"
                                          HorizontalAlignment="Left" VerticalAlignment="Center"                              
                                          Content="{TemplateBinding ComboBox.SelectionBoxItem}" 
                                          ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}"
                                          ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}">
                            </ContentPresenter>

                            <TextBox Name="txtInput" Margin="3, 3, 23, 3" IsReadOnly="{TemplateBinding IsReadOnly}" Visibility="Hidden" Background="Transparent" TextChanged="txtInput_TextChanged" SelectionChanged="txtInput_SelectionChanged"                                         
                                HorizontalAlignment="Stretch" VerticalAlignment="Center" TextAlignment="Left" Focusable="True" GotFocus="txtInput_GotFocus" LostFocus="txtInput_LostFocus">
                                <TextBox.Template>
                                    <ControlTemplate TargetType="TextBox">
                                        <Border Name="PART_ContentHost" Focusable="False"/>
                                    </ControlTemplate>
                                </TextBox.Template>
                            </TextBox>

                            <!-- Popup showing items -->
                            <Popup Name="PART_Popup" Placement="Bottom" Focusable="False" AllowsTransparency="True" IsOpen="{TemplateBinding ComboBox.IsDropDownOpen}" PopupAnimation="Slide">
                                <Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding FrameworkElement.ActualWidth}" MaxHeight="{TemplateBinding ComboBox.MaxDropDownHeight}">
                                    <Border Name="DropDownBorder" Background="White" Margin="0, 1, 0, 0"
                                        CornerRadius="0" BorderThickness="1,1,1,1" 
                                        BorderBrush="{StaticResource BasicNormalBorderBrush}"/>
                                    <ScrollViewer Margin="4" SnapsToDevicePixels="True">
                                        <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained" />
                                    </ScrollViewer>
                                </Grid>
                            </Popup>
                        </Grid>
                        ......

Solution

  • Switch PART_ContentHost type from Border to ScrollViewer in TextBox ControlTemplate

    <TextBox Name="txtInput" Margin="3, 3, 23, 3" IsReadOnly="{TemplateBinding IsReadOnly}" Visibility="Hidden" Background="Transparent" TextChanged="txtInput_TextChanged" SelectionChanged="txtInput_SelectionChanged"                                         
        HorizontalAlignment="Stretch" VerticalAlignment="Center" TextAlignment="Left" Focusable="True" GotFocus="txtInput_GotFocus" LostFocus="txtInput_LostFocus">
        <TextBox.Template>
            <ControlTemplate TargetType="TextBox">
                <ScrollViewer Name="PART_ContentHost" Focusable="False"/>
            </ControlTemplate>
        </TextBox.Template>
    </TextBox>