对TextBox使用样式滚动条 [英] Using a styles Scrollbar for TextBox
问题描述
你好,
我想为文本框设置垂直滚动条的样式.
我不允许添加/使用自动用于所有文本框的样式,
或scrollviewers,因为不允许我覆盖任何常见的内容.
因此,我需要一种可以在每个文本框中(也可以不可以)使用的键/名称样式.
这是我的XAML代码,用于设置文本框的样式:
Hello,
I want to style a vertical scrollbar for my textbox.
I am NOT allowed to add/use a style that is automatically used for all textboxes,
or scrollviewers, because I am not allowed to override anything common.
So I need a style with a key/name I can use for each textbox (or not).
Here my XAML code for styling the textbox:
<Style x:Key="PortalTextBox" TargetType="{x:Type TextBox}">
<setter property="OverridesDefaultStyle" value="true" />
<setter property="AllowDrop" value="true" />
<setter property="MinWidth" value="0" />
<setter property="MinHeight" value="0" />
<setter property="FocusVisualStyle" value="{x:Null}" />
<setter property="Template">
<setter.value>
<controltemplate targettype="{x:Type TextBox}">
<!--<scrollviewer focusable="false" removed="Transparent" x:name="PART_ContentHost" horizontalscrollbarvisibility="Hidden" verticalscrollbarvisibility="Hidden" style="{StaticResource {ComponentResourceKey ResourceId=PortalScrollViewer, TypeInTargetAssembly=SiControls:SiCResourceKeys}}" xmlns:x="#unknown" />-->
<!--<scrollcontentpresenter x:name="PART_ScrollContentPresenter" cancontentscroll="{TemplateBinding CanContentScroll}" canhorizontallyscroll="False" canverticallyscroll="False" contenttemplate="{TemplateBinding ContentTemplate}" content="{TemplateBinding Content}" grid.column="0" margin="{TemplateBinding Padding}" grid.row="0" xmlns:x="#unknown" />-->
<scrollbar x:name="PART_VerticalScrollBar" automationproperties.automationid="VerticalScrollBar" cursor="Arrow" grid.column="1" minimum="0" grid.row="0" visibility="{TemplateBinding VerticalScrollBarVisibility}" value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" viewportsize="{TemplateBinding ActualHeight}" style="{StaticResource {ComponentResourceKey ResourceId=PortalVerticalScrollBarStyle, TypeInTargetAssembly=SiControls:SiCResourceKeys}}" xmlns:x="#unknown" />
</controltemplate>
</setter.value>
</setter>
</Style>
我已经有可用于PortalVerticalScrollBarStyle和PortalHorizontalScrollBarStyle的样式(在ScrollViewer上).
我尝试设置文本框的样式,然后:
I have already Styles for PortalVerticalScrollBarStyle and PortalHorizontalScrollBarStyle which are working (on a ScrollViewer).
I try to style the Textbox then:
<textbox x:name="txtAdditionalInstructions" horizontalalignment="Stretch" verticalalignment="Stretch" uselayoutrounding="True" fontsize="16" text="{Binding CurrentOrder.AdditionalInstructions, ElementName=AddExtraInformation}" height="322" acceptsreturn="True" acceptstab="True" margin="4,0,0,0" borderthickness="0" horizontalscrollbarvisibility="Hidden" verticalscrollbarvisibility="Auto" width="316" textwrapping="Wrap" style="{DynamicResource {ComponentResourceKey ResourceId=PortalTextBox, TypeInTargetAssembly=SiControls:SiCResourceKeys}}" xmlns:x="#unknown"></textbox>
但是不知何故,它不起作用.
我也不确定texbox的PART_Scroll ..东西必须是什么样子.
知道我在做什么错吗?非常感谢!
But somehow it is not working.
I am also not sure for the texbox how the PART_Scroll.. stuff must look like.
Any idea what I am doing wrong? Thanks a lot!
推荐答案
解决方案:
Solution:
<Style x:Key="PortalTextBox" TargetType="{x:Type TextBox}">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="MinWidth" Value="0"/>
<Setter Property="MinHeight" Value="0"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Microsoft_Windows_Themes:ListBoxChrome x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" SnapsToDevicePixels="true">
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Style="{StaticResource {ComponentResourceKey ResourceId=PortalScrollViewer, TypeInTargetAssembly=SiControls:SiCResourceKeys}}"/>
</Microsoft_Windows_Themes:ListBoxChrome>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这篇关于对TextBox使用样式滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!