2016-12-20 6 views
1

私はListBoxを持っていましたが、ヘッダーを修正できませんでしたので、残りのページと一緒にスクロールしません。 は理由を探した後、私はすべてがContentControlに経由して配置され、メインウィンドウで、1 ScrollViewerのがあることを見出し、すべての周りに包まれた:私はちょうど働いているので、私は本当に、それを削除することはできません内からスクロールビューアを無効にすることはできますか?

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

1つのコンテンツページで、このプロジェクトに後で実装される予定です。 このScrollViewerを自分のウィンドウ内から無効にすることはできますか?それとも何とか私のコンテンツに影響を与えないようにしますか?

編集:明確にする。このコード(上記)は私が編集できないMainWindowにあります。内容は.csでcontent.Content = new (content class)で追加されます。

<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> 

を私は得ることができない、より高いレベルでそれarount包まれた1があるため、問題は、このScrollViewerのdoesntの仕事ということ、である:私は働いているこれらのクラスの一つで、私が持っています...

+0

あなたが何であるかを私たちに伝えることができますヒア?どのようにそれを修正したいですか? – WPFUser

答えて

1

ScrollViewerの周囲にスクロールを行う理由がない場合があります。代わりに、リストボックスの高さを制限してスクロールを提供してください:

<!-- 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> 

高さを50として使用しました。もちろん、これはあなたの希望の高さに置き換えてください。動的にしたい場合は、StackPanelTextBlockActualHeightにはMultibindingを使用し、(ActualHeightStackPanel) - ()を返すコンバータを使用できます。

はEDIT:あなたの編集中のコードにこれを適用する:

<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番号を固定あなたのListBoxとしてScrollViewerその高さ、スクロールを提供します
  • は、ListBoxの高さを50に制限しています。希望の高さに置き換えてください。それを動的にしたい場合は、GridStackPanelActualHeightMultibindingを使用し、() - ()を返すコンバータを使用できます。私はこれを行うための方法を見つける傾けるので
  • Grid
1

HeightためBindingを追加しました、私はこのAttachedPropertyを提案しています。このAttachedPropertyを使用してParentView ScrollViewerを無効にすることができます。これは、要件を満たすための方法の1つです。

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> 

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); 
    } 
} 
関連する問題