pankaj
pankaj

Reputation: 207

How to set height to the dynamically added user control in stackpanel?

We are having wpf user control (UserControl.xaml) in one dll (Child.dll).

We added the reference of "Child.dll" in to another application (ParentApplication)

In parent application we have one stackpanel in which we have to add the user control dynamically using the user control constructor.

ParentApplication contains :

MainWindow.xaml

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:fa="http://schemas.fontawesome.io/icons/" x:Class="ParentApplication.MainWindow"
        xmlns:Mvis="clr-namespace:Mvis;assembly=Mvis"
        Title="MainWindow" Height="1000" Width="1000" WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid>
        <TabControl>
<TabItem Cursor="Hand">
                <TabItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="ChildApplication"/>
                    </StackPanel>
                </TabItem.Header>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanContentScroll="True" Grid.Row="0" >
                        <StackPanel x:Name="userControlPlaceHolder" Background="Blue"/>
                    </ScrollViewer>
                </Grid>
            </TabItem>
        </TabControl>
</Grid>
</Window>

So we doing it like this on MainWindow.xaml.cs :

public partial class MainWindow : Window
{
        public MainWindow()
        {
            InitializeComponent();
            UserControl userControl = new UserControl();
            this.userControlPlaceHolder.Children.Add(userControl);           
        }
}

Issue 1: User control is displayed only in half height of the screen.

**Note: We did not set any height to UserControl.xaml and we can not set the height control height like userControl.Height=200; **

Issue 2: We have used scroll viewer in MainWindow.xaml, but when we resize the application window, vertical scroll bar is not displayed.

Upvotes: 1

Views: 1373

Answers (1)

mm8
mm8

Reputation: 169200

Remove the StackPanel and add the UserControl directly to the ScrollViewer:

<TabItem Cursor="Hand">
    <TabItem.Header>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="ChildApplication"/>
        </StackPanel>
    </TabItem.Header>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <ScrollViewer x:Name="sv" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanContentScroll="True" Grid.Row="0" >

        </ScrollViewer>
    </Grid>
</TabItem>

this.sv.Content = userControl;        

ScrollViewers and StackPanels don't work well together:

Horizontal scroll for stackpanel doesn't work

Upvotes: 2

Related Questions