使用滚动

时间:2017-12-12 03:38:48

标签: c# wpf scroll combobox textbox

可编辑的wpf组合框。 输入长字符串时,文本框不会滚动存在问题。 喜欢这个链接 Editable combobox text scrolling

TextBox.ScrollToHome()但它不起作用。

这是我的代码和 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>
                        ......

1 个答案:

答案 0 :(得分:0)

PART_ContentHost Border

中将ScrollViewer类型从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>