gpt4 book ai didi

c# - 带滚动的组合框文本输入

转载 作者:太空宇宙 更新时间:2023-11-03 14:58:15 25 4
gpt4 key购买 nike

可编辑的 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>
......

最佳答案

开关 PART_ContentHostBorder 输入至 ScrollViewerTextBox 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>

关于c# - 带滚动的组合框文本输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47764988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com