How to can set a scrollviewer in a usercontrol to the standart one ya can access from VisualStudio

Stefan Schmidt 96 Reputation points
2021-10-09T04:18:28.327+00:00

Hello,

i created a Usercontrol with a ScrollViewer inside. I would prefer if i would add my user control into an application and would c set on my user control the properties of the vertical and horizontal scrollbar visibility , it would change the scrollviewer inside my user control,, also is the one that i would mean if i would change properties in the Properties Panel from Visual Studio. How can i reference the ScrollViewer in my CustomControl to be the standard ScrolllViewer?

<UserControl x:Class="_PreviewPanel.PreviewPanel"

<ScrollViewer HorizontalScrollBarVisibility="{Binding HorizontalScrollBarVisibility}"> 
<Grid x:Name="LayoutRoot"/>
<ScrollViewer>
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,182 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 14,796 Reputation points Microsoft Employee
    2021-10-11T03:03:54.34+00:00

    You could customize dependency properties(ShowScrollbar) for UserControl and bind HorizontalScrollBarVisibility to the dependency property.
    Then you can change the HorizontalScrollBarVisibility of ScrollViewer by setting the custom dependency property (ShowScrollbar) of UserControl in the property panel of the wpf program. The code is as follows.

    The code of UserControl:

    <UserControl x:Class="WpfControlLibrary1.UserControl1"  
                 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:WpfControlLibrary1"  
                 mc:Ignorable="d"   
                 d:DesignHeight="400" d:DesignWidth="300">  
    
        <ScrollViewer x:Name="scroll" HorizontalScrollBarVisibility="{Binding ShowScrollbar}">  
            <StackPanel Name="LayoutRoot" Background="AliceBlue">  
                <TextBlock Height="80" Width="320" Background="PaleGoldenrod"/>  
                <TextBlock Height="80" Background="PaleGoldenrod"/>  
                <TextBlock Height="80" Background="PaleGoldenrod"/>  
                <TextBlock Height="80" Background="PaleGoldenrod"/>  
                <TextBlock Height="80" Background="PaleGoldenrod"/>  
                <TextBlock Height="80" Background="PaleGoldenrod"/>  
            </StackPanel>  
        </ScrollViewer>  
    </UserControl>  
    

    The code of UserControl.cs:

    using System.Windows;  
    using System.Windows.Controls;  
    
    namespace WpfControlLibrary1  
    {  
        public partial class UserControl1 : UserControl  
        {  
            public UserControl1()  
            {  
                InitializeComponent();  
            }  
        public static readonly DependencyProperty ShowScrollbarProperty =  
      DependencyProperty.Register("ShowScrollbar", typeof(bool), typeof(UserControl1),  
        new FrameworkPropertyMetadata(new PropertyChangedCallback(AdjustControl)));  
    
        public bool ShowScrollbar  
        {  
          get  
          {  
            return (bool)GetValue(ShowScrollbarProperty);  
          }  
          set  
          {  
            SetValue(ShowScrollbarProperty, value);  
          }  
        }  
        private static void AdjustControl(DependencyObject source, DependencyPropertyChangedEventArgs e)  
        {  
          (source as UserControl1).UpdateControls((bool)e.NewValue);  
        }  
    
        private void UpdateControls(bool showScrollbar)  
        {  
          scroll.HorizontalScrollBarVisibility = (showScrollbar ? ScrollBarVisibility.Visible : ScrollBarVisibility.Hidden);  
        }  
      }  
    }  
    

    The code of xaml:

     <Window  
                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:CustomScrollViewer"  
                xmlns:WpfControlLibrary1="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1" x:Class="CustomScrollViewer.MainWindow"  
                mc:Ignorable="d"  
                Title="MainWindow" Height="450" Width="800">  
            <Grid>  
                <WpfControlLibrary1:UserControl1 HorizontalAlignment="Left" Height="100"  Margin="117,165,0,0" VerticalAlignment="Top" Width="100" ShowScrollbar="True"/>  
            </Grid>  
        </Window>  
    

    The picture of result:
    139258-5.png


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html