TextBox样式不创建垂直滚动条

时间:2010-11-04 18:56:41

标签: c# wpf

我正在尝试为文本框创建样式,但是当我将此模板添加到样式时,垂直滚动条会消失。

<Style x:Key="MyTextBoxStyle" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
        <Setter Property="Foreground" Value="{DynamicResource BlueForegroundBrush}"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="FontSize" Value="14"/>
        <Setter Property="Padding" Value="0,-5,0,0"/>
        <Setter Property="Width" Value="120"/>
        <Setter Property="Height" Value="100"/>
        <Setter Property="Focusable" Value="True"/>
        <Setter Property="AllowDrop" Value="True"/>
        <Setter Property="IsReadOnly" Value="True" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Grid>
                        <Rectangle x:Name="_rct" Stroke="#FFA8AFBE" RadiusX="8" RadiusY="8" Fill="White" />
                        <Border x:Name="_borderActive" BorderThickness="2" CornerRadius="8" BorderBrush="#FFA8AFBE" > 
                            <ListBox x:Name="Bd" SnapsToDevicePixels="true"
                                    Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    Margin="6">
                                <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            </ListBox>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

1 个答案:

答案 0 :(得分:0)

我注意到您将ScrollViewer包裹在ListBox中。 ListBox的默认模板包含ScrollViewer,因此它们之间的互动最有可能导致ScrollBar问题。

您希望将ScrollViewer放在ListBox TextBox模板中,以实现目标?如果我们知道您为什么这样做,我们可以帮助您实现所需的行为。

默认模板使用主题程序集中的ListBoxChrome来实现主题边框外观。如果这就是您所追求的,这将有效(请注意,您需要添加对PresentationFramework.Aero的引用):

<mwt:ListBoxChrome xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
                   Background="{TemplateBinding Background}" 
                   BorderBrush="{TemplateBinding BorderBrush}"
                   BorderThickness="{TemplateBinding BorderThickness}" 
                   RenderMouseOver="{TemplateBinding IsMouseOver}" 
                   RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" 
                   SnapsToDevicePixels="True">
    <ScrollViewer Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 
</mwt:ListBoxChrome>