Home > Enterprise >  WPF scrollviewer for adding dynamic elements
WPF scrollviewer for adding dynamic elements

Time:05-19

I need help with my WPF application. In the bottom of my window I have a canvas which is filled dynamically with groupboxes, which contain several userlements like buttons and gridviews. The inserted content is outside my window and I want to access it with scrollbars but the scrollbars aren't adjusting so I can't access the elements.

How can I reach the content with the scrollbars?

Thanks for your help.

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible"  >
    <Grid Height="Auto">
        <GroupBox Header="Fleet Import" HorizontalAlignment="Left" Height="115" Margin="23,21,0,0" VerticalAlignment="Top" Width="419">
            <StackPanel Margin="0,0,-67,0">
                <StackPanel Orientation="Horizontal">
                    <Button x:Name="btnSelectProject" Content="Select Project" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="88" Height="23" Click="btnSelectProject_Click"/>
                    <TextBox Name="tbPath" Height="17" Width="283" Margin="10,10,0,0" />
                </StackPanel>

                <Button x:Name="bntConnectTia" Content="Connect to TIA" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="88" Height="23" Click="bntConnectTia_Click"/>
            </StackPanel>
        </GroupBox>
        <GroupBox Header="Start update" HorizontalAlignment="Left" Height="115" Margin="498,21,0,0" VerticalAlignment="Top" Width="277">
            <StackPanel Orientation="Horizontal">
                <StackPanel>
                    <RadioButton Content="Maintanace"  HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="88" Height="23"/>
                    <RadioButton Content="Production"  HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="88" Height="23"/>
                </StackPanel>
                <StackPanel>
                    <Button Content="Start Update" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="88" Height="23" Click="Button_Click_2"/>
                    <Button Content="Cancel"  HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="88" Height="23"/>
                </StackPanel>
            </StackPanel>
        </GroupBox>
        <GroupBox Header="GroupBox" HorizontalAlignment="Left" Height="164" Margin="23,157,0,0" VerticalAlignment="Top" Width="752">
            <TextBox HorizontalAlignment="Left" Height="128" TextWrapping="Wrap" Name="tbOutput" VerticalAlignment="Top" Width="742" Margin="0,0,-2,0"/>

        </GroupBox>
        <Canvas x:Name="canvasDevices" Margin="23,346,25,10" Height="Auto" />
        <GroupBox Header="GroupBox" HorizontalAlignment="Left" Height="164" Margin="-393,337,0,-439" VerticalAlignment="Top" Width="752" Visibility="Hidden">
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Button Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75"/>
                    <TextBox HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
                    <Button Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75"/>
                </StackPanel>
                <DataGrid Margin="20,10,20,0" RenderTransformOrigin="0.163,0.543" Height="88"/>
            </StackPanel>

        </GroupBox>
        <Button Content="Button" HorizontalAlignment="Left" Margin="89,151,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_3"/>
    </Grid>
</ScrollViewer>

GroupBox groupbox = new GroupBox();
        groupbox.Header = deviceName;
        groupbox.Name = deviceName;
        groupbox.Width = 742;
        groupbox.Height = 250;

stackPanelV.Children.Add(stackPanelH);
            stackPanelV.Children.Add(dgDevice);
            groupbox.Content = stackPanelV;
            Canvas.SetTop(groupbox,0);
            canvasDevices.Children.Add(groupbox);

CodePudding user response:

Your items get out the canvas bounds, canvas is not resized and ScrollView not applied.

By the way I dont recommend you using canvas for that prupose. There is plenty of other layouts that will give you a better result such as StackPanel or WrapPanel, and if you give to WrapPanel a width it will nicely rearrange your items.

  • Related