question

StefanSchmidt-6201 avatar image
0 Votes"
StefanSchmidt-6201 asked HuiLiu-MSFT commented

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

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-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered HuiLiu-MSFT commented

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 to enable e-mail notifications if you want to receive the related email notification for this thread. 


5.png (15.8 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@StefanSchmidt-6201. May I know if you have any chance to check my answer? I am glad to help if you have any other questions.

0 Votes 0 ·