当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to make WrapPanel to show vertical scrollbar when children are full with or without ScrollViewer

问题描述:

I have a WrapPanel and buttons are programmatically created and added as children of the WrapPanel. So, I want to show vertical scrollbar when the WrapPanel is full of buttons (children) to be able to add more buttons continuously.

If we need a scrollbar shown, do we have to bring ScrollViewer? Isn't there a way without ScrollViewer? What I want to get is, because the WrapPanel is of small size, I want a scrollbar to be shown only when needed (like full of children).

My code is simple as below (WrapPanel inside Grid and the Grid is inside TabControl)

Many thanks always for your excellence.

Update: I struggled in finding solution on internet for even several days. And I tried put WrapPanel inside ScrollViewer. However, though I set the VerticalScroll to auto, the vertical scrollbar is always shown even when the WrapPanel doesn't have any children.

Furthermore, when I intentionally make the WrapPanel full of children (buttons), the vertical scrollbar of ScrollViewer doesn't provide scrolldown availability.

And the buttons at the bottom line of WrapPanel shown cut and more, I can't scroll down to see beyond the button at the bottom line. I made buttons to be placed beyond the bottom line of WrapPanel intentionally.

With or without, I want the vertical scrollbar to be shown when only needed (full of children). It seems very easy to be done. But it's difficult to make it work properly.

Solution: was 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 which make button programmatically and add as a child of WrapPanel.

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

网友答案:

The idea in WPF is that every component has only its own job and if you want certain behavior, you combine multiple components to create the view you are looking for.

This means that in order to get a scroll bar for a panel, you will have to wrap it in a ScrollViewer component. That’s the purpose of the ScrollViewer and that’s the only (sane) solution to solve this.


However, though I set the verticalscroll to auto, the verticalscrollbar is always shown even when the Wrappanel doesn't have any child […]

Then you seem to be using the ScrollViewer incorrectly, or wrapping the wrong element. It should look like this:

<ScrollViewer VerticalScrollBarVisibility="Auto">
    <WrapPanel>
        <!-- Any number of components here -->
    </WrapPanel>
</ScrollViewer>

If I place lots of example labels inside that, then I do get a scroll bar as soon as the window is not large enough to show them all. But if there is enough room, the scroll bar is not displayed.

Note that the ScrollViewer itself needs to have the proper dimensions in the parent element, so make sure that it’s not larger than the visible area. It is also necessary for the WrapPanel (or whatever other element you wrap with the ScrollViewer) to have auto widths and heights. Otherwise, with fixed dimensions, the dimensions of the panel will not change as you modify the panel’s content and as such the scrolling status will not change.


See this complete example with a dynamic number of elements:

<Window x:Class="WpfExampleApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="300" Width="200">
    <ScrollViewer VerticalScrollBarVisibility="Auto">
        <WrapPanel Name="panel">
            <Button Click="Button_Click">Add child</Button>
        </WrapPanel>
    </ScrollViewer>
</Window>

Code-behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Label element = new Label() { Content = "This is some example content" };
        panel.Children.Add(element);
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: