【问题标题】:ScrollViewer control template breaks textboxesScrollViewer 控件模板打破文本框
【发布时间】:2012-11-30 14:54:23
【问题描述】:

我从 MSDN (link) 复制了 ScrollViewer 模板,它破坏了文本框的行为。当我使用鼠标选择文本时,现在滚动不会跟随选择。为什么?如何解决?

这是我的代码

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="350"
        Width="525">
  <Window.Resources>
    <Style TargetType="{x:Type ScrollViewer}">
      <Setter Property="OverridesDefaultStyle"
              Value="True" />
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ScrollViewer}">
            <Grid>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition />
              </Grid.ColumnDefinitions>
              <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="Auto" />
              </Grid.RowDefinitions>
              <Border Grid.Column="1"
                      BorderThickness="0,1,1,1">
                <Border.BorderBrush>
                  <SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
                </Border.BorderBrush>
                <ScrollContentPresenter />
              </Border>
              <ScrollBar x:Name="PART_VerticalScrollBar"
                         Value="{TemplateBinding VerticalOffset}"
                         Maximum="{TemplateBinding ScrollableHeight}"
                         ViewportSize="{TemplateBinding ViewportHeight}"
                         Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" />
              <ScrollBar x:Name="PART_HorizontalScrollBar"
                         Orientation="Horizontal"
                         Grid.Row="1"
                         Grid.Column="1"
                         Value="{TemplateBinding HorizontalOffset}"
                         Maximum="{TemplateBinding ScrollableWidth}"
                         ViewportSize="{TemplateBinding ViewportWidth}"
                         Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" />
            </Grid>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </Window.Resources>
  <StackPanel>
    <TextBox Margin="20"
             Width="100"
             Text="sdjkfhsdjkfhjkdshxcmvnm,xcnv,mxcnv, mxcnv,mxcngjklsdjkfh jkdfghjkfhgjkdfhgkjdfghkjdfhgbkjcvhbkcvjbh" />
  </StackPanel>
</Window>

我使用 .NET 4

【问题讨论】:

    标签: c# .net wpf xaml controltemplate


    【解决方案1】:

    我遇到了同样的问题,我终于通过在 ScrollContentPresenter 中添加 CanContenScroll="True" 解决了。

    <ScrollContentPresenter Grid.Column="0" CanContentScroll="True"/>
    

    如果你想让它更灵活,只需将其绑定到祖先值:

     <ScrollContentPresenter Grid.Column="0" 
                             CanContentScroll="{TemplateBinding CanContentScroll}" />
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      是的,我得到了同样的行为。恐怕我无法真正弄清楚为什么会发生这种情况,这可能是 WPF 中的错误或他们定义控件模板的方式。

      TextBox 的控件模板有一个滚动查看器作为其布局的一部分,显然该模板以某种方式干扰了它。

      我能提出的唯一建议是您为您的样式设置一个 x:Key 值,并且仅在您需要的特定情况下在 ScrollViewer 元素上引用该样式,而不是将其应用于所有 ScrollViewer。然后,TextBox 的元素树中包含的 ScrollViewer 将无法识别它。有点痛苦,你失去了 WPF 模板的动态特性,但它应该可以工作。

      【讨论】:

        【解决方案3】:

        您的样式针对WindowScrollViewer 类型的ALL 控件,因为该样式没有设置键(x:Key) - 这是一个问题,因为TextBox 实际上在其模板中使用ScrollViewer

        尝试使用&lt;Style x:Key="MyScrollViewerStyle" TargetType="{x:Type ScrollViewer}"&gt;。然后,您可以在 ScrollViewer 上引用此样式,如下所示:&lt;ScrollViewer Style="{StaticResource MyScrollViewerStyle}"&gt;

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多