我可以从内部禁用scrollviewer吗?

时间:2016-12-20 08:58:13

标签: c# wpf xaml

我有一个ListBox,但我无法修复标题,所以它不会与页面的其余部分一起滚动。 在找到原因后,我发现在主窗口中,通过ContentControl放置所有内容,有一个ScrollViewer包裹着所有内容:

<ScrollViewer Margin="0,0,0,0" >   
    <ContentControl x:Name="content" Margin="0,0,0,0"/>
</ScrollViewer>

我无法真正删除它,因为我正在一个内容页面上工作,稍后将在该项目中实现。 是否可以从我的窗口中禁用此ScrollViewer?或者以某种方式让它停止影响我的内容?

编辑:说清楚。这段代码(上面)在MainWindow中,我无法编辑。内容以content.Content = new (content class)的.cs格式添加。在其中一个我正在研究的课程中,我有:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="20" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="1" Orientation="Horizontal">
        <TextBlock Text ={x:Static ...}>
        <Text Block ...>
    </StackPanel>           

    <ScrollViewer Grid.Row="2">
        <ListBox>...</ListBox>
    </ScrollViewer>
</Grid>

问题是,这个滚动查看器不起作用,因为有一个包裹在更高级别上,我无法得到...

3 个答案:

答案 0 :(得分:1)

也许您可以通过周围df['count'] = df['Coordinates'].apply(len) // 2 没有理由提供滚动的方式调整内容的大小。相反,通过限制其高度来提供ListBox中的滚动:

ScrollViewer

我用50作为高度。当然,这应该用你想要的高度代替。如果您想要它是动态的,您可以<!-- When the StackPanel is not bigger than the ScrollViewer, there should be no reason for it to scroll your title out of sight. If there is Padding in your scroll viewer, an additional converter may bre required to modify the height accordingly --> <StackPanel Height="{Binding ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ScrollViewer}}"> <TextBlock Text="TitleForMyListBox"/> <ListBox Height="50"> <!-- ... --> </ListBox> </StackPanel> 使用MultibindingActualHeight的{​​{1}}以及返回StackPanel TextBlock的转换器}) - (ActualHeight} StackPanel

编辑:将其应用于编辑中的代码:

ActualHeight

我有:

  • 修正了TextBlock个数字
  • 删除了<Grid Height="{Binding ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ScrollViewer}}"> <Grid.RowDefinitions> <RowDefinition Height="20" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Orientation="Horizontal"> <TextBlock Text ={x:Static ...}> <Text Block ...> </StackPanel> <ListBox Grid.Row="1">...</ListBox> </Grid> ,因为Grid.Row在高度有限时会提供滚动
  • ScrollViewer的高度限制为50.再次,这应该用您想要的高度代替。如果您想要它是动态的,您可以ListBox使用ListBoxMultibinding的{​​{1}}以及返回ActualHeight Grid的转换器}) - (StackPanel} ActualHeight
  • Grid
  • ActualHeight添加了StackPanel

答案 1 :(得分:1)

由于我无法找到办法,我建议AttachedProperty。您可以使用此ScrollViewer禁用ParentView AttachedProperty。这是实现您的要求的一种方式。

 public class ScrollViewerExtension : DependencyObject
{

    public static bool GetDisableParentScrollViewer(DependencyObject obj)
    {
        return (bool)obj.GetValue(DisableParentScrollViewerProperty);
    }

    public static void SetDisableParentScrollViewer(DependencyObject obj, bool value)
    {
        obj.SetValue(DisableParentScrollViewerProperty, value);
    }

    // Using a DependencyProperty as the backing store for DisableParentScrollViewer.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DisableParentScrollViewerProperty =
        DependencyProperty.RegisterAttached("DisableParentScrollViewer", typeof(bool), typeof(ScrollViewerExtension), new PropertyMetadata(false,OnDisableParentScrollViewerChanged));

    private static void OnDisableParentScrollViewerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is FrameworkElement)
        {
            (d as FrameworkElement).Loaded += (_, __) =>
              {
                  var scrollViewer = FindAncestor(d as Visual, typeof(ScrollViewer)) as ScrollViewer;
                  if (scrollViewer != null && (bool)e.NewValue)
                      scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
              };
        }
    }

    public static Visual FindAncestor(Visual startingFrom, Type typeAncestor)
    {
        if (startingFrom != null)
        {
            DependencyObject parent = VisualTreeHelper.GetParent(startingFrom);

            while (parent != null && !typeAncestor.IsInstanceOfType(parent))
            {
                parent = VisualTreeHelper.GetParent(parent);
            }

            return parent as Visual;
        }

        return null;
    }
}

并在您看来,

<Grid attached:ScrollViewerExtension.DisableParentScrollViewer="True">
<Grid.RowDefinitions>
    <RowDefinition Height="20" />
    <RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="1" Orientation="Horizontal">
    <TextBlock Text ={x:Static ...}>
    <Text Block ...>
</StackPanel>           

<ScrollViewer Grid.Row="2">
    <ListBox>...</ListBox>
</ScrollViewer>

答案 2 :(得分:1)

  

是否可以在我的窗口中禁用此ScrollViewer?或者以某种方式让它停止影响我的内容?

您可以处理控件的Loaded事件,在可视树中找到父ScrollViewer并使用ScrollViewer.SetVerticalScrollBarVisibility方法禁用它。

<强> Window.xaml:

<Window x:Class="WpfApplication1.Window14"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="Window14" Height="300" Width="300">
    <Grid>
        <ScrollViewer Margin="0,0,0,0" Height="200" >
            <ContentControl x:Name="content" Margin="0,0,0,0">
                <ContentControl.Content>
                    <local:UserControl1 />
                </ContentControl.Content>
            </ContentControl>
        </ScrollViewer>
    </Grid>
</Window>

<强> UserControl1.xaml:

<UserControl x:Class="WpfApplication1.UserControl2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication1"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Background="Yellow" ScrollViewer.VerticalScrollBarVisibility="Hidden">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Rectangle Height="100" Fill="Green" />
        <ListBox x:Name="lv" Grid.Row="1">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>

<强> UserControl1.xaml.cs:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
        this.Loaded += UserControl1_Loaded;
        lv.ItemsSource = Enumerable.Range(0, 1000);
    }

    private void UserControl1_Loaded(object sender, RoutedEventArgs e)
    {
        ScrollViewer sv = FindParent<ScrollViewer>(this);
        if (sv != null)
        {
            ScrollViewer.SetVerticalScrollBarVisibility(sv, ScrollBarVisibility.Disabled);
        }
    }

    private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
    {
        var parent = VisualTreeHelper.GetParent(dependencyObject);

        if (parent == null) return null;

        var parentT = parent as T;
        return parentT ?? FindParent<T>(parent);
    }
}