代码之家  ›  专栏  ›  技术社区  ›  Jerry Nixon

WPF/XAML自定义ScrollViewer滚动条,内部控件(如TextBox)中没有自定义滚动条

  •  2
  • Jerry Nixon  · 技术社区  · 14 年前

    谢谢你调查我的问题。我想在ScrollViewer中自定义滚动条。没问题。但是等等。当我这样做的时候,它也改变了内部控件的滚动条。我不想影响那些滚动条。如何指定正确的范围?

    下面是几乎可以工作的XAML:

    <Page
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <ScrollViewer>  
        <ScrollViewer.Resources>
          <Style TargetType="{x:Type ScrollBar}">
            <Setter Property="Background" Value="Red" />
          </Style>
        </ScrollViewer.Resources>
        <TextBox Height="200" Width="200" VerticalScrollBarVisibility="Visible" />
      </ScrollViewer>
    </Page>
    
    1 回复  |  直到 13 年前
        1
  •  2
  •   Zamboni    14 年前

    因为ScrollViewer只支持一个子对象,所以我添加了一个网格来包装textbox。 在我的例子中,我应用了一个覆盖样式来使文本框变成蓝色。 如果从网格中删除整个setter,则会得到默认值。

    <Page 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
        <Grid>
            <ScrollViewer>
                <ScrollViewer.Resources>
                    <Style TargetType="{x:Type ScrollBar}">
                        <Setter Property="Background" Value="Red" />
                    </Style>
                </ScrollViewer.Resources>
                <Grid>
                    <Grid.Resources>
                        <Style TargetType="{x:Type ScrollBar}">
                            <!-- remove setter to get default -->
                            <Setter Property="Background" Value="Blue" />
                        </Style>
                    </Grid.Resources>
                    <TextBox Height="200" Width="200" VerticalScrollBarVisibility="Visible" />
                </Grid>    
            </ScrollViewer>
        </Grid>
    </Page>