WPF Scrollviewer的起始位置很奇怪

时间:2013-09-12 15:41:01

标签: c# wpf xaml

我有一个带有2个标签的窗口,每个标签包含一个具有相同属性的Scrollviewer

<ScrollViewer VerticalScrollBarVisibility="Auto"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CanContentScroll="False">
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
    </Grid>
</ScrollViewer>

现在差异:

第一个标签:

网格行0包含一个<WrapPanel>,内部有<StackPanel>个所有控件

网格行1包含<DataGrid>,填充其余部分并具有最小高度

第二个标签:

网格行0包含一个WrapPanel,内部有<DockPanel>个所有控件

网格行1包含<DataGrid>,填充其余部分并具有最小高度

问题:

当我打开窗口时,有太多的控件无法一次查看所以滚动条或<ScrollViewer>出现,这是完美的。但由于某些尴尬的原因,第二个标签滚动条不在顶部,而是在窗口下方略超过3/4。我想知道以前是否有人讨论过这个问题并且可能知道我忘记了什么?

我尝试了什么:

  • 将有问题的<ScrollViewer>命名并调用表单加载MyScroll.ScrollToTop();无法正常工作,但我想这是因为控件尚不存在。

  • 我尝试将GotFocus事件添加到<TabItem>但每次我点击其中的控件时都会触发此宝贝。当我第一次点击窗口尽头的<ComboBox>时,我发现这很有趣。

在尝试了所有我发现在<ScrollViewer>被拳头查看时从头顶开始是我想要的,并想知道是否有办法做到这一点或我是否遗漏了什么。

其次我实际上希望<ScrollViewer>滚动到顶部,当人们导航<TabItem>时,我的小错误让我意识到我真的喜欢它。

用户很少会在该窗口上使用超过2个选项卡,因此不需要疯狂的解决方案

编辑: 我找到了一些东西。如果我向上滚动我有问题的选项卡,然后再选择另一个选项卡并回来,滚动返回到它被窃听的地方。

请注意,对于选定的组合框和文本框的值,只有简单的绑定,我没有任何事件。 o是和网格的数据源,但此时两个标签都是空的。

我有4个窗口,格式相同,12个窗口中只有1个。

编辑#2:这里是完整的xaml

<syncfusion:ChromelessWindow x:Class="prjSelection.Crating.frmCratingWestManufacturers"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:prjSelection.Crating"   
    Title="West Crating" Height="640" Width="1024"
    UseNativeChrome="True"
    xmlns:syncfusion="http://schemas.syncfusion.com/wpf"  
    syncfusion:SkinStorage.VisualStyle="Metro" ShowActivated="True" WindowStartupLocation="CenterScreen">
<Grid>
    <Grid Name="grdOverlay" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent" Panel.ZIndex="99999999" Visibility="Hidden" MinWidth="150" MinHeight="200">
        <Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Fill="White" Opacity="0.8"></Rectangle>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <Label HorizontalAlignment="Stretch" VerticalAlignment="Center" Content="Running Selection" FontSize="18" FontWeight="Bold"></Label>
            <syncfusion:SfBusyIndicator AnimationType="Gear" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ViewboxHeight="150" ViewboxWidth="150" VerticalContentAlignment="Center" />
        </StackPanel>
    </Grid>
    <syncfusion:TabControlExt BorderThickness="0,2,0,0"  Name="tab1"  AllowDrop="False" Background="White" EnableLabelEdit="False" AllowDragDrop="False" CloseButtonType="Hide" DefaultContextMenuItemVisibility="Hidden" SelectOnCreatingNewItem="False" ShowTabItemContextMenu="False" ShowTabListContextMenu="False" SnapsToDevicePixels="True" TabPanelBackground="White" UseLayoutRounding="False" TabScrollButtonVisibility="Auto" TabVisualStyle="None" TabStripPlacement="Top" TabItemSelectedForeground="White" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch">
        <syncfusion:TabItemExt  Header="File" Width="150" IconMargin="0" >
            <ScrollViewer VerticalScrollBarVisibility="Auto"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CanContentScroll="False">
                <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <WrapPanel Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Top">
                        <StackPanel>
                            <GroupBox Header="File Visual Properties" VerticalContentAlignment="Stretch" VerticalAlignment="Top" Name="grpFileVisualProperties">
                                <WrapPanel Orientation="Horizontal">
                                    <DockPanel Height="25" LastChildFill="True" Width="220" Margin="2,2,0,0">
                                        <Label Content="File Color" Height="25" Name="lblFileColor" />
                                        <ComboBox DisplayMemberPath="Value" Height="25" ItemsSource="{Binding Path=File.Color}" Name="cboFileColor" SelectedItem="{Binding Path=File.SelectedColor, Mode=TwoWay}" Width="120" DockPanel.Dock="Right" />
                                        <Label />
                                    </DockPanel>
                                </WrapPanel>
                            </GroupBox>
                            <GroupBox Header="File Relative Properties" VerticalContentAlignment="Stretch" VerticalAlignment="Top" Name="grpFileRelativeProperties">
                                <WrapPanel Orientation="Horizontal">
                                    <DockPanel Height="25" LastChildFill="True" Width="280" Margin="2,2,0,0">
                                        <Label Content="File Type" Height="25" Name="lblFileType" />
                                        <ComboBox DisplayMemberPath="Value" Height="25" ItemsSource="{Binding Path=File.FileType}" Name="cboFileType" SelectedItem="{Binding Path=File.SelectedFileType, Mode=TwoWay}" Width="120" DockPanel.Dock="Right" />
                                        <Label />
                                    </DockPanel>                                       
                                </WrapPanel>
                            </GroupBox>
                        </StackPanel>
                    </WrapPanel>

                    <Grid Grid.Row="1" HorizontalAlignment="Stretch" Name="grid2" VerticalAlignment="Stretch">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="10" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>

                        <Button Content="Run Selection" FontSize="20" FontStretch="Normal" Height="46" Name="cmdFileSelection" Padding="0" Width="198.162" Canvas.Left="793.838" Canvas.Top="410" VerticalAlignment="Top" Grid.Column="2" />
                        <syncfusion:SfDataGrid AllowGrouping="False" AllowResizingColumns="False" AllowSorting="False" ColumnSizer="Auto" FontSize="13" GroupRowSelectionBrush="{x:Null}" ItemsSource="{Binding Path=File.ResultGrid}" Name="grdFileData" NavigationMode="Cell" ShowColumnWhenGrouped="False" VerticalContentAlignment="Stretch" Canvas.Left="898.447" Canvas.Top="462" HorizontalAlignment="Left" Grid.Column="0" MinHeight="120" MinWidth="120" />

                    </Grid>
                </Grid>
            </ScrollViewer>
        </syncfusion:TabItemExt>
        <syncfusion:TabItemExt Header="Box" Width="150" IconMargin="0">
            <ScrollViewer VerticalScrollBarVisibility="Auto"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CanContentScroll="False">
                <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>   

                    <WrapPanel Grid.Row="0" HorizontalAlignment="Stretch" Orientation="Horizontal">
                        <DockPanel Height="25" Width="375" Margin="2,2,0,0">
                            <Label Content="Height" Name="lblHeight" Height="25" />
                            <ComboBox ItemsSource="{Binding Path=Box.Height}" SelectedItem="{Binding Path=Box.SelectedHeight, Mode=TwoWay}" DisplayMemberPath="Value" Name="cboHeight" Width="200" Height="25" DockPanel.Dock="Right" />
                            <Label />
                        </DockPanel>                         
                    </WrapPanel>

                    <Grid Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="10" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>

                        <Button Content="Run Selection" FontSize="20" FontStretch="Normal" Height="46" Name="cmdBoxSelection" Padding="0" Width="198.162" Canvas.Left="793.838" Canvas.Top="410" VerticalAlignment="Top" Grid.Column="2" />
                        <syncfusion:SfDataGrid AllowGrouping="False" AllowResizingColumns="False" AllowSorting="False" ColumnSizer="Auto" FontSize="13" GroupRowSelectionBrush="{x:Null}" ItemsSource="{Binding Path=Box.ResultGrid}" Name="grdBoxData" NavigationMode="Cell" ShowColumnWhenGrouped="False" VerticalContentAlignment="Stretch" Canvas.Left="898.447" Canvas.Top="462" HorizontalAlignment="Left" Grid.Column="0" MinHeight="120" MinWidth="120" />

                    </Grid>
                </Grid>
            </ScrollViewer>
        </syncfusion:TabItemExt>
    </syncfusion:TabControlExt>
</Grid>

enter image description here

1 个答案:

答案 0 :(得分:0)

解决方案是修改XAML的编写顺序。 WPF将焦点放在XAML代码中的第一个元素,无论它是否是最后一个元素