I have a WrapPanel, and buttons are programmatically created and added as children of the WrapPanel. So, I want to show a vertical scrollbar when the WrapPanel is full of buttons (children) in order to be able to add additional buttons continuously.
If we need a scrollbar, do we need to bring a ScrollViewer? Is there no way without a ScrollViewer? What I want to get because the WrapPanel is small, I want the scroll bar to be displayed only when it was necessary (for example, full of children).
My code is simple as shown below (WrapPanel inside Grid and Grid inside TabControl)
Many thanks always for your excellence.
Update: I have been struggling to find a solution on the Internet for several days now. And I tried installing WrapPanel inside ScrollViewer. However, although I set the vertical bus to auto, the vertical scroll bar is always displayed, even if the WrapPanel has no children.
Also, when I intentionally make the WrapPanel full of children (buttons), the ScrollViewer vertical scrollbar does not provide scroll accessibility. And the buttons on the bottom line of the WrapPanel are shown to cut and more, I can not scroll down to see the button on the bottom line. I made buttons to place outside the bottom line of the WrapPanel intentionally.
With or without, I want the vertical scrollbar to be displayed only when it is needed (full of children). It seems very easy to do. But it's hard to get it to work properly.
Solution: provided by Mr. Henk Holterman
<DropShadowEffect/> </Button.Effect> </Button> <WrapPanel x:Name="WrapPanelGreen" HorizontalAlignment="Left" Height="180" VerticalAlignment="Top" Width="232" UseLayoutRounding="True" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto"/> </Grid> </TabItem> </TabControl>
And below is my simple code that makes a program programmatically and adds a WrapPanel as a child.
for (int k = 0; k < Overviews.Length; k++) { Button btnoverviewcontent = new Button(); ToolTip tt = new ToolTip(); tt.Content = "Press this button if you want to modify or delete."; btnoverviewcontent.ToolTip = tt; btnoverviewcontent.Cursor = Cursors.Hand; SolidColorBrush mySolidColorBrush = new SolidColorBrush(); mySolidColorBrush.Color = Color.FromArgb(255, 101, 173, 241); btnoverviewcontent.Background = mySolidColorBrush; btnoverviewcontent.Effect = new DropShadowEffect { Color = new Color { A = 255, R = 0, G = 0, B = 0 }, Direction = 315, ShadowDepth = 5, Opacity = 1 }; btnoverviewcontent.Padding = new Thickness(3, 3, 3, 3); btnoverviewcontent.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch; TextBlock textBlock = new TextBlock() { Text = Overviews[k], TextAlignment = TextAlignment.Left, TextWrapping = TextWrapping.Wrap, }; btnoverviewcontent.Content = textBlock; btnoverviewcontent.BorderThickness = new Thickness(0, 0, 0, 0); btnoverviewcontent.FontStretch = FontStretches.UltraExpanded; btnoverviewcontent.Margin = new Thickness(5, 5, 5, 5); WrapPanelGreen.Children.Add(btnoverviewcontent); btnoverviewcontent.Click += new RoutedEventHandler(OnOverviewClick);