不显示WPF数据网格的水平滚动

时间:2013-08-14 06:49:04

标签: wpf datagrid scroll

水平滚动条不会显示在下面的数据网格中。我想在用户调整窗口大小时进行水平滚动。 在下面的情况下,我在窗口大小调整后无法看到datagrid的正确部分。

 <Grid Name="gridAllRecipes" DockPanel.Dock="Top" AllowDrop="True" Background="White" >
            <DataGrid Name="dtAllRecipes" HorizontalScrollBarVisibility="Auto"  ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" Focusable="True"  MinWidth="1050" PreviewDrop="panelAllRecipe_PreviewDrop" AutoGenerateColumns="False" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" CanUserAddRows="False" AreRowDetailsFrozen="False"  CanUserDeleteRows="False" CanUserReorderColumns="False" ColumnWidth="*" CanUserResizeColumns="False" CanUserResizeRows="True" CanUserSortColumns="False" HeadersVisibility="None" GridLinesVisibility="Horizontal" VerticalGridLinesBrush="#FFDFDFDF" HorizontalGridLinesBrush="#FFCCCCCC" BorderThickness="0" MouseDoubleClick="dtAllRecipes_MouseDoubleClick" KeyUp="dtAllRecipes_KeyUp" SelectionChanged="dtAllRecipes_SelectionChanged" PreviewKeyDown="dtAllRecipes_PreviewKeyDown">
                <DataGrid.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">

                        <Style.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True">
                                <Setter Property="Background" Value="#bdd6ec">
                                </Setter>
                                <Setter Property="BorderThickness" Value="0"></Setter>
                                <Setter Property="TextBlock.Foreground" Value="White"></Setter>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="False">
                                <Setter Property="Background" Value="White"/>
                                <Setter Property="BorderThickness" Value="0"></Setter>
                            </DataTrigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="#e7f2fc" />
                                <!--<Setter Property="Background" Value="#d9e9f7" />-->
                            </Trigger>
                        </Style.Triggers>
                        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                        <EventSetter Event="MouseEnter" Handler="EventSetter_OnHandler1" />
                        <EventSetter Event="MouseLeave" Handler="EventSetter_OnHandler2" />
                    </Style>

                </DataGrid.CellStyle>
                <DataGrid.Columns>
                    <DataGridTemplateColumn x:Name="Template12" >
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Grid Name="grid1">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="103" />
                                        <ColumnDefinition Width=".90*" />
                                    </Grid.ColumnDefinitions>
                                    <Border Margin="10,0,0,0" CornerRadius="7" HorizontalAlignment="Center" VerticalAlignment="Center">
                                        <Grid>
                                            <Border Name="mask" Background="White" CornerRadius="7"/>
                                            <StackPanel>
                                                <StackPanel.OpacityMask>
                                                    <VisualBrush Visual="{Binding ElementName=mask}"/>
                                                </StackPanel.OpacityMask>
                                                <Image Grid.Column="0" Height="83" HorizontalAlignment="Center" Stretch="Fill"  MaxHeight="83"  MaxWidth="83" Name="imgRecipe" VerticalAlignment="Center" Width="83" Source="{Binding Photo}" />
                                            </StackPanel>
                                        </Grid>
                                    </Border>
                                    <!--<Image Grid.Column="0" Height="83" HorizontalAlignment="Left"  MinWidth="83" Margin="10,0,0,0" MaxWidth="83" Name="imgRecipe" Stretch="Fill" VerticalAlignment="Center" Width="83" Source="{Binding Photo}" />-->
                                    <Grid Grid.Column="1">
                                        <StackPanel Orientation="Horizontal">
                                            <Label Name="lblRecipeName" Content="{Binding RecipeNameFullName}" Visibility="Collapsed"   />
                                            <TextBox IsReadOnly="True" Style="{DynamicResource AllRecipeTextTitle}" Tag="{Binding RecipeId}" Background="Transparent" Foreground="#000000"  BorderThickness="0" Text="{Binding RecipeName}" Height="28" HorizontalAlignment="Left" Margin="0,6,0,0" Name="txtRecipeName" VerticalAlignment="Top" LostFocus="txtRecipeName_LostFocus" MouseDoubleClick="txtRecipeName_MouseDoubleClick" GotFocus="txtRecipeName_GotFocus">
                                            </TextBox>
                                            <Image Height="25" Tag="{Binding RecipeId}" Source="{Binding FavouritesImage}" Visibility="{Binding isFavouritesImageVisible}" HorizontalAlignment="Left" Margin="10,5,0,0" Name="imgAllFavourite" Stretch="Fill" VerticalAlignment="Top" Width="25" MouseDown="imgAllFavourite_MouseDown"  />
                                        </StackPanel>
                                        <Grid Margin="0,26,0,0">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width=".30*" />
                                                <ColumnDefinition Width=".35*" />
                                                <ColumnDefinition Width=".16*" />
                                                <ColumnDefinition Width=".19*" />
                                            </Grid.ColumnDefinitions>
                                            <Label Grid.Column="0" Content="{Binding Source}" Foreground="#000000"  HorizontalAlignment="Left" Margin="-2,0,0,0" Name="lblSource" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabel}" FontStyle="Normal" />
                                            <StackPanel Orientation="Horizontal" Grid.Column="1">
                                                <Label  Content="Cuisine" Visibility="{Binding isCuisinesVisible}"  HorizontalAlignment="Right" Name="label5" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabel}" Foreground="#7e848d" FontStyle="Normal" />
                                                <Label  Content="{Binding Cuisine}"  Visibility="{Binding isCuisinesVisible}"  HorizontalAlignment="Right" Margin="5,0,0,0" Name="lblCuisine" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabel}" Foreground="#7e848d" FontStyle="Normal" />
                                            </StackPanel>
                                            <StackPanel Orientation="Horizontal" Grid.Column="2">
                                                <Label Content="Servings"   Visibility="{Binding isServingsVisible}"  HorizontalAlignment="Left" Margin="0,0,0,0" Name="label11" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabel}" Foreground="#7e848d" FontStyle="Normal" />
                                                <Label Content="{Binding Servings}" Visibility="{Binding isServingsVisible}"  HorizontalAlignment="Left" Margin="5,0,0,0" Name="lblServings" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabel}" Foreground="#7e848d" FontStyle="Normal" />
                                            </StackPanel>
                                        </Grid>
                                        <Grid Margin="0,48,0,0">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width=".30*" />
                                                <ColumnDefinition Width=".35*" />
                                                <ColumnDefinition Width=".16*" />
                                                <ColumnDefinition Width=".19*" />
                                            </Grid.ColumnDefinitions>
                                            <StackPanel Orientation="Horizontal" Grid.Column="0">
                                                <Label Content="Difficulty: "  HorizontalAlignment="Left" Margin="-2,0,0,0" Name="label3" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabel}" Foreground="#7e848d" FontStyle="Oblique" />
                                                <Label Content="{Binding Difficulty}"  Width="170" HorizontalAlignment="Left" Margin="-7,0,0,0" Name="lblDifficulty" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabel}" Foreground="#7e848d" FontStyle="Italic" />
                                            </StackPanel>
                                            <StackPanel Orientation="Horizontal" Grid.Column="1">
                                                <Label Content="Dish Type" Visibility="{Binding isDishTypeVisible}"  HorizontalAlignment="Left"   Name="label7" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabelDishTypeHeader}" Foreground="#7e848d" FontStyle="Normal" />
                                                <Label Content="{Binding DishType}" Visibility="{Binding isDishTypeVisible}"  HorizontalAlignment="Left" Name="lblDishtype" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabelDishType}" Foreground="#7e848d" FontStyle="Normal" />
                                            </StackPanel>
                                            <StackPanel Orientation="Horizontal" Grid.Column="2"  >
                                                <Label Content="Prep Time" Visibility="{Binding isPrepTimeVisible}"   HorizontalAlignment="Right" Margin="-10,0,0,0" Name="label12" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabel}" Foreground="#7e848d" FontStyle="Normal"/>
                                                <Label Content="{Binding PrepTime}" Visibility="{Binding isPrepTimeVisible}"  HorizontalAlignment="Right" Margin="5,0,0,0" Name="lblPreTime" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabel}" Foreground="#7e848d" FontStyle="Normal"/>
                                            </StackPanel>
                                            <StackPanel Orientation="Horizontal" Grid.Column="3" HorizontalAlignment="Right">
                                                <Label Content="Created" Visibility="{Binding isCreatedDateVisible}" HorizontalAlignment="Right" Margin="0,0,0,0" Name="label17" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabel}" Foreground="#7e848d" FontStyle="Normal"/>
                                                <Label Content="{Binding CreationDate}" Visibility="{Binding isCreatedDateVisible}"  HorizontalAlignment="Right" Margin="4,0,0,0" Name="lblCreatedDate" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabel}" Foreground="#7e848d" FontStyle="Normal" />
                                            </StackPanel>
                                        </Grid>

                                        <Grid Margin="0,73,0,0">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width=".30*" />
                                                <ColumnDefinition Width=".35*" />
                                                <ColumnDefinition Width=".16*" />
                                                <ColumnDefinition Width=".19*" />
                                            </Grid.ColumnDefinitions>
                                            <StackPanel Orientation="Horizontal" Grid.Column="0">
                                                <!--<Image Height="19" Source="{Binding RatingImage}" HorizontalAlignment="Left"  Margin="2,0,0,0" Name="imgRating" Stretch="Fill" VerticalAlignment="Top" Width="111" />-->
                                                <Image Height="19" Tag="{Binding RecipeId}" HorizontalAlignment="Left" Margin="2,0,0,0" Name="imgAllRating1" Stretch="Fill" VerticalAlignment="Top"  Width="21" Source="{Binding RatingOneImage}" MouseLeftButtonDown="imgAllRating1_MouseLeftButtonDown" />
                                                <Image Height="19" Tag="{Binding RecipeId}" HorizontalAlignment="Left" Margin="2,0,0,0" Name="imgAllRating2" Stretch="Fill" VerticalAlignment="Top" Width="21" Source="{Binding RatingTwoImage}" MouseLeftButtonDown="imgAllRating2_MouseLeftButtonDown" />
                                                <Image Height="19" Tag="{Binding RecipeId}" HorizontalAlignment="Left" Margin="2,0,0,0" Name="imgAllRating3" Stretch="Fill" VerticalAlignment="Top" Width="21" Source="{Binding RatingThreeImage}" MouseLeftButtonDown="imgAllRating3_MouseLeftButtonDown" />
                                                <Image Height="19" Tag="{Binding RecipeId}" HorizontalAlignment="Left" Margin="2,0,0,0" Name="imgAllRating4" Stretch="Fill" VerticalAlignment="Top" Width="21" Source="{Binding RatingFourImage}" MouseLeftButtonDown="imgAllRating4_MouseLeftButtonDown" />
                                                <Image Height="19" Tag="{Binding RecipeId}" HorizontalAlignment="Left" Margin="2,0,0,0" Name="imgAllRating5" Stretch="Fill" VerticalAlignment="Top" Width="21" Source="{Binding RatingFiveImage}" MouseLeftButtonDown="imgAllRating5_MouseLeftButtonDown" />
                                            </StackPanel>
                                            <StackPanel Orientation="Horizontal" Grid.Column="1">
                                                <Label Content="Main Ingredients" Visibility="{Binding isMainIngredientsVisible}"  HorizontalAlignment="Left" Name="label9" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabelMainIngredientHeader}" Foreground="#7e848d" FontStyle="Normal" />
                                                <Label Content="{Binding MainIngredient}" Visibility="{Binding isMainIngredientsVisible}"  HorizontalAlignment="Left"  Name="lblMainIngredients" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabelMainIngredient}" Foreground="#7e848d" FontStyle="Normal" />
                                            </StackPanel>
                                            <StackPanel Orientation="Horizontal" Grid.Column="2">
                                                <Label Content="Cook Time"  Visibility="{Binding isCookTimeVisible}"  HorizontalAlignment="Left" Margin="-14,-3,0,0" Name="label13" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabel}" Foreground="#7e848d" FontStyle="Normal" />
                                                <Label Content="{Binding CookingTime}"  Visibility="{Binding isCookTimeVisible}" HorizontalAlignment="Left" Margin="5,-3,0,0" Name="lblCookTime" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabel}" Foreground="#7e848d" FontStyle="Normal"/>
                                            </StackPanel>
                                            <StackPanel Orientation="Horizontal" Grid.Column="3" HorizontalAlignment="Right">
                                                <Label Content="Modified" Visibility="{Binding isModifiedDateVisible}"  HorizontalAlignment="Right" Margin="0,-3,0,0" Name="label18" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabel}" Foreground="#7e848d" FontStyle="Normal"/>
                                                <Label Content="{Binding LastModifiedDate}" Visibility="{Binding isModifiedDateVisible}" HorizontalAlignment="Right" Margin="4,-3,0,0" Name="lblModifiedDate" VerticalAlignment="Top"  Style="{DynamicResource AllGridLabel}" Foreground="#7e848d" VerticalContentAlignment="Stretch" FontStyle="Normal" />
                                            </StackPanel>
                                        </Grid>
                                        <Image Height="28" Tag="{Binding RecipeId}" Source="{Binding FlaggedImage}" Visibility="{Binding isFlagImageVisible}" HorizontalAlignment="Right" Margin="0,-1,-1,0" Name="imgFlagged" Stretch="Fill" VerticalAlignment="Top" Width="28" MouseDown="imgFlagged_MouseDown" />
                                    </Grid>
                                </Grid>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
            <TextBlock Name="txtMessage" Text="No Recipes" Padding="0,150,0,0" FontSize="26" HorizontalAlignment="Stretch"  Visibility="Collapsed" Foreground="#E0E0E0" FontWeight="SemiBold" />
        </Grid>

请让我知道显示水平滚动条的解决方案

1 个答案:

答案 0 :(得分:1)

我认为问题是你使用Grid作为外部容器,这将自动增长以适应其内容,因此内部DataGrid从未认为它太小并显示滚动条。

请尝试使用DockPanel