Scrollviewer扩展器无法正常工作

时间:2013-08-24 16:02:47

标签: c# wpf scrollviewer expander

滚动查看器运行良好,直到我添加了一个扩展器控件,现在终生我不能让它工作。已经删除了stackpanel,并且从我读过的内容中指定了一个无限高度。我仍然在掌握WPF,所以任何帮助都会非常感激。

嵌套子控件:

<UserControl x:Class="BloodHound.Controls.ucDocumentEmail"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid Name="grdSearchEmail" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
          Height="Auto" Width="Auto" Margin="15,15,15,15" >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"  />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />

        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="Auto" ></ColumnDefinition>
            <ColumnDefinition Width="*" ></ColumnDefinition>
            <ColumnDefinition Width="Auto" ></ColumnDefinition>
            <ColumnDefinition Width="*" ></ColumnDefinition>

        </Grid.ColumnDefinitions>

        <StackPanel Orientation="Horizontal" Grid.Column="0" Grid.Row="0" >
            <Image Source="/Images/email.png" />
            <Label FontWeight="Bold" >EMail</Label>
        </StackPanel>

        <Label Grid.Column="1" Grid.Row="0" >Created Date</Label>
        <TextBox Name="txtDateCreated" Grid.Column="2" Grid.Row="0" Grid.ColumnSpan="2" ></TextBox>


        <Label Grid.Column="0" Grid.Row="1" >From</Label>
        <TextBox Name="txtFrom" Grid.Column="1" Grid.Row="1" ></TextBox>


        <Label Grid.Column="2" Grid.Row="1" >To</Label>
        <TextBox Name="txtTo" Grid.Column="3" Grid.Row="1"  ></TextBox>

        <Label Grid.Column="0" Grid.Row="2" >Subject</Label>
        <TextBox Name="txtSubject" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="3"  ></TextBox>


        <Expander Header="Email Details" 
                  Width="Auto" Height="Auto" ExpandDirection="Down" 
                  HorizontalAlignment="Stretch" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="10" 
                  VerticalAlignment="Top" MaxHeight="300" >
            <Border Background="LightGray" Grid.Column="0" Grid.Row="0"
        BorderBrush="Black" 
        BorderThickness="1" Height="Auto" HorizontalAlignment="Stretch"
        CornerRadius="15" 
        Padding="5" VerticalAlignment="Top" >
                    <Grid Name="grdTrans" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
              Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="10" Height="Auto" >
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" ></ColumnDefinition>
                            <ColumnDefinition Width="*" ></ColumnDefinition>
                            <ColumnDefinition Width="*" ></ColumnDefinition>
                            <ColumnDefinition Width="*" ></ColumnDefinition>
                        </Grid.ColumnDefinitions>


                        <Label Grid.Column="0" Grid.Row="0" >CC</Label>
                        <TextBox Name="txtCC" Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="3" ></TextBox>

                        <Label Grid.Column="0" Grid.Row="1" >Importance</Label>
                        <TextBox Name="txtImportance" Grid.Column="1" Grid.Row="1" ></TextBox>

                        <Label Grid.Column="2" Grid.Row="1" >Folder</Label>
                        <TextBox Name="txtFolder" Grid.Column="3" Grid.Row="1" ></TextBox>

                        <Label Grid.Column="0" Grid.Row="2" >Received By</Label>
                        <TextBox Name="txtReceivedBy" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="3" ></TextBox>

                        <Label Grid.Column="0" Grid.Row="4" >Body</Label>
                        <TextBox Grid.Column="1" Grid.ColumnSpan="3" MaxHeight="150" 
                 ScrollViewer.VerticalScrollBarVisibility="Auto" 
                 TextWrapping="Wrap" AcceptsReturn="True" Grid.Row="4" 
                 Name="txtBody" Height="Auto" Width="Auto" 
                 HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" ></TextBox>


                        <Label Grid.Column="0" Grid.Row="5" >Attachments</Label>
                        <ListView Name="lvwAttach" Grid.Column="1" Grid.Row="5" Grid.ColumnSpan="4" ></ListView>


                    </Grid>
            </Border>
        </Expander>
    </Grid>
</UserControl>

然后是父控制:

<UserControl x:Class="BloodHound.Controls.ucDocumentListing"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid Name="grdDocList" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="21"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="*" ></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0">
            <Label>Search for keyword</Label>
            <TextBox Name="txtSearch" Width="65" Margin="15,5,5,5" ></TextBox>
            <Button x:Name="btnSearch" Margin="5,5,5,5"
                                Click="btnSearch_Click" ToolTip="Search for Keyword" >
                <StackPanel Orientation="Horizontal">
                    <Image Source="/Images/magnifier_zoom_in.png" />
                    <Label>Search</Label>
                </StackPanel>
            </Button>
            <Button x:Name="btnAddNew" Margin="25,5,5,5"
                                Click="btnAddNew_Click" ToolTip="Add New File/s" >
                <StackPanel Orientation="Horizontal">
                    <Image Source="/Images/add.png" />
                    <Label>Add File/s</Label>
                </StackPanel>
            </Button>
        </StackPanel>

        <ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1" Grid.Column="0" >
            <Grid Name="grdDocs" Grid.Column="0" Grid.Row="1"  Margin="15,15,15,15"  >
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"  />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" ></ColumnDefinition>
                </Grid.ColumnDefinitions>

            </Grid>
        </ScrollViewer>
    </Grid>
</UserControl>

然后将它加载到一个窗口中:

<Window x:Class="BloodHound.TestDocumentList"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TestDocumentList" Height="300" Width="300" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"   >
    <Grid Name="grdTest" Loaded="grdTest_Loaded"  VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"  />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />

        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="*" ></ColumnDefinition>
        </Grid.ColumnDefinitions>
    </Grid>
</Window>

背后的代码:

private void grdTest_Loaded(object sender, RoutedEventArgs e)
{
    Controls.ucDocumentListing ctl = new Controls.ucDocumentListing();
    ctl.EntityID = 1;
    ctl.EntityTypeID = 1;
    ctl.LoadControl();

    RowDefinition newRow = new RowDefinition();
    newRow.Height = new GridLength(0, GridUnitType.Auto);
    grdTest.RowDefinitions.Insert(grdTest.RowDefinitions.Count - 1, newRow);

    Controls.ucSearchEmail em = new Controls.ucSearchEmail();

    int rowIndex = grdTest.RowDefinitions.Count - 2;

    Grid.SetRow(ctl, rowIndex);
    Grid.SetColumn(ctl, 1);
    Grid.SetRowSpan(ctl, 1);
    Grid.SetColumnSpan(ctl, 1); // Change this if you want.
    grdTest.Children.Add(ctl);

}

0 个答案:

没有答案