WPF ScrollViewer错误

时间:2017-01-31 09:10:42

标签: c# wpf scrollviewer

当我的窗口最大化时,滚动查看器出现问题。 在网格中使用它似乎是错误的空间。 滚动条改变大小,在底部“跳转”到最后并且不会出现更多。

我的代码

<UserControl x:Class="MainPage.ListPage"
  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" 
  xmlns:local="clr-namespace:MainPage"
  xmlns:p="clr-namespace:MainPage.Properties"
  mc:Ignorable="d"
  d:DesignHeight="340.984" d:DesignWidth="390.079">
<UserControl.Resources>
    <!--Styles -->
    <Style x:Key="headerStyle" TargetType="DataGridColumnHeader">
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Stretch"/>
        <Setter Property="Height" Value="35"/>
        <Setter Property="TextElement.FontSize" Value="16"/>
        <Setter Property="TextElement.FontWeight" Value="Bold"/>
        <Setter Property="BorderThickness" Value="1,1,1,2"/>
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="Background" Value="White"/>
        <Setter Property="Visibility" Value="Visible"/>
    </Style>
    <Style x:Key="detailTableStyle" TargetType="{x:Type Label}">
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
    </Style>
    <Style x:Key="baseStyle" TargetType="{x:Type FrameworkElement}">
        <Setter Property="TextElement.FontSize" Value="14" />
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="TextElement.FontFamily" Value="Arial Nova"/>
    </Style>
    <Style x:Key="cellStyle" TargetType="TextBlock">
        <Setter Property="TextAlignment" Value="Center" />
        <Setter Property="VerticalAlignment" Value="Center" />
    </Style>
    <!-- Templates -->
    <DataTemplate x:Key="deviationTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Rectangle Grid.Column="0" Fill="{Binding LW}" HorizontalAlignment="Right" Width="{Binding LowTolRectangle}" Height="25" Margin="5,5,0,5"></Rectangle>
            <Rectangle Grid.Column="1" Fill="{Binding HT}" HorizontalAlignment="Left" Width="{Binding HighTolRectangle}" Height="25" Margin="0,5,5,5"></Rectangle>
            <TextBlock Grid.ColumnSpan="2" Text="{Binding D}" Style="{StaticResource baseStyle}"/>
        </Grid>
    </DataTemplate>
</UserControl.Resources>
<DataGrid x:Name="GP" AutoGenerateColumns="False" IsReadOnly="True" HeadersVisibility="Column" ColumnHeaderStyle="{StaticResource headerStyle}" VerticalGridLinesBrush="Black" HorizontalGridLinesBrush="Black" BorderBrush="Black" ItemsSource="{Binding MP}" VerticalAlignment="Top" Style="{StaticResource baseStyle}">
    <DataGrid.Resources>
        <local:BindingProxy x:Key="proxy" Data="{Binding}" />
    </DataGrid.Resources>
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsVisible}" Value="False">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
            <Setter Property="IsSelected" Value="{Binding Path=Selected, Mode=TwoWay}" />
        </Style>
    </DataGrid.RowStyle>
    <DataGrid.Columns>
        <DataGridTextColumn Header="{x:Static p:Resources.P}" Width="*" Binding="{Binding Path=P}" ElementStyle="{StaticResource cellStyle}" />
        <DataGridTextColumn Header="{x:Static p:Resources.LT}" Width="*" Binding="{Binding Path=LT}" ElementStyle="{StaticResource cellStyle}" />
        <DataGridTemplateColumn Header="{x:Static p:Resources.D}" Width="*" CellTemplate="{StaticResource deviationTemplate}"/>
        <DataGridTextColumn Header="{x:Static p:Resources.HT}" Width="*" Binding="{Binding Path=HT}" ElementStyle="{StaticResource cellStyle}"/>
        <DataGridTextColumn Header="{x:Static p:Resources.RP}" Width="*" Binding="{Binding Path=RG}" ElementStyle="{StaticResource cellStyle}" Visibility="{Binding Data.Visibility, Source={StaticResource proxy}}"/>
    </DataGrid.Columns>
    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <StackPanel Grid.Column="0">
                    <Border Margin="10" Width="Auto">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" MinWidth="50"></ColumnDefinition>
                                <ColumnDefinition Width="Auto" MinWidth="50"></ColumnDefinition>
                                <ColumnDefinition Width="Auto" MinWidth="50"></ColumnDefinition>
                                <ColumnDefinition Width="Auto" MinWidth="50"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"></RowDefinition>
                                <RowDefinition Height="Auto"></RowDefinition>
                                <RowDefinition Height="Auto"></RowDefinition>
                            </Grid.RowDefinitions>
                            <Label Grid.Column="0" Grid.Row="0" Content="" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="1" Grid.Row="0" Content="X" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="2" Grid.Row="0" Content="Y" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="3" Grid.Row="0" Content="Z" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="0" Grid.Row="1" Content="{x:Static p:Resources.Nom}" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="0" Grid.Row="2" Content="{x:Static p:Resources.Mea}" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="1" Grid.Row="1" Content="{Binding PT[0]}" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="2" Grid.Row="1" Content="{Binding PT[1]}" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="3" Grid.Row="1" Content="{Binding PT[2]}" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="1" Grid.Row="2" Content="{Binding NP[0]}" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="2" Grid.Row="2" Content="{Binding NP[1]}" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="3" Grid.Row="2" Content="{Binding NP[2]}" Style="{StaticResource detailTableStyle}"/>
                        </Grid>
                    </Border>
                </StackPanel>
                <StackPanel Grid.Column="1">
                    <Border Margin="20,10" Width="Auto">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" MinWidth="50"></ColumnDefinition>
                                <ColumnDefinition Width="Auto" MinWidth="50"></ColumnDefinition>
                                <ColumnDefinition Width="Auto" MinWidth="50"></ColumnDefinition>
                                <ColumnDefinition Width="Auto" MinWidth="50"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"></RowDefinition>
                                <RowDefinition Height="Auto"></RowDefinition>
                            </Grid.RowDefinitions>
                            <Label Grid.Column="0" Grid.Row="0" Content="" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="0" Grid.Row="1" Content="{x:Static p:Resources.Nor}" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="1" Grid.Row="0" Content="DX" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="2" Grid.Row="0" Content="DY" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="3" Grid.Row="0" Content="DZ" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="1" Grid.Row="1" Content="{Binding aa[0]}" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="2" Grid.Row="1" Content="{Binding aa[1]}" Style="{StaticResource detailTableStyle}"/>
                            <Label Grid.Column="3" Grid.Row="1" Content="{Binding aa[2]}" Style="{StaticResource detailTableStyle}"/>
                        </Grid>
                    </Border>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>
</DataGrid>

1 个答案:

答案 0 :(得分:0)

StackPanelScrollViewer并没有很好地融合,看起来您的StackPanel在您的代码中没用,只需将Grid.Column直接设置在您的边框中,这应该可以解决问题。

此示例代码是您ScrollViewer元素的子代码吗?

<强>更新

手动执行此操作的一种简单方法是执行此操作:

<DockPanel>
    <ScrollViewer VerticalScrollBarVisibility="Auto" DockPanel.Dock="Top">
        // Your DataGrid
    </ScrollViewer>
</DockPanel>

希望这个帮助