2

我的 DataGrid 出现在可视范围之外的数据。我在 StackOverflow 上查看了几个类似的问题,但似乎都没有解决我的问题。我无法弄清楚我做错了什么。这就是我的 xaml 的样子:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>        
    <TextBlock HorizontalAlignment="Left" Foreground="{DynamicResource TitleFontColor}" 
     TextWrapping="Wrap" Text="{Binding WindowTitle}" VerticalAlignment="Center" 
     Margin="10,10,0,10" FontSize="18.667" FontFamily="Segoe UI" />
    <Border  Background="{DynamicResource pnlworkarea}" 
      Grid.Row="1" CornerRadius="2" BorderThickness="1">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="65"/>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Border BorderThickness="1" 
         Background="{DynamicResource Brush_HeaderNew}" 
         BorderBrush="{DynamicResource pnlworkareastroke}" 
         CornerRadius="2" Margin="5">
        <Grid Margin="0">
               <ListBox ItemsSource="{Binding ViewMenuItems}" SelectedItem="{Binding Viewselected}" 
                VerticalAlignment="Center" Style="{DynamicResource Content_Header_ListBoxStyle}" 
        ItemTemplate="{DynamicResource Header_DataTemplate}" 
        ItemContainerStyle="{DynamicResource ContentHeader_ListBoxItemStyle}"               
        ItemsPanel="{DynamicResource Header_ItemsPanelTemplate}" HorizontalAlignment="Left"/>                                   
    </Grid>
    </Border>
    <Grid Grid.Row="1" Margin="5">
            <StackPanel HorizontalAlignment="Left" Orientation="Horizontal" VerticalAlignment="Top" Margin="10,0,0,0">
                     <ContentControl Focusable="False" Content="ContentControl"  
        Background="{DynamicResource Brush_VectorIcon}" 
       Template="{DynamicResource vector_Summary}" 
       Height="16" Margin="0,5,0,0"/>
        <TextBlock TextWrapping="Wrap" Text="{Binding SearchDisplayMessage}" Margin="5,3,0,0" VerticalAlignment="Center"/>
            </StackPanel>
        </Grid>
        <Grid Grid.Row="2" Margin="5">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <DataGrid  x:Name="dataGrid"
                    commands:ControlEvents.GridViewSelectionChangedEventCommand="{Binding SelectionItemsChangeCommand}"
                    SelectionMode="Single"
                    AutoGenerateColumns="True"                      
                    Margin="0,5" Grid.Row="1" IsReadOnly="True"
                    ItemsSource="{Binding GridItem, Mode=TwoWay}"  SelectedItem="{Binding SelectedItem}"
                    AlternatingRowBackground="#FFF5F4F8" Background="White" Grid.ColumnSpan="2"
                    ScrollViewer.CanContentScroll="True" 
                    ScrollViewer.HorizontalScrollBarVisibility="Auto" 
                    ScrollViewer.VerticalScrollBarVisibility="Auto">
                    <i:Interaction.Behaviors>
                        <Behaviors:SelectorDoubleClickCommandBehavior Command="{Binding GridDoubleclickcommand}"/>
                    </i:Interaction.Behaviors>

                </DataGrid>
            </Grid>

        </Grid>

    </Border>

</Grid>

只是要补充一点,这里的 DataGrid 不是我正在使用的实际 DataGrid。我的自定义 DataGrid 是从 DataGrid 派生的。但我也用普通的 DataGrid 试过这个,但似乎也不起作用。

4

4 回答 4

5

我有一个类似的问题,水平滚动条没有显示。我有一列宽度为“*”。一旦我删除它,滚动条就会出现。我想如果您将宽度设置为固定,它也会出现。

于 2016-04-27T06:53:28.770 回答
2

您的高度为 Auto

<Grid Grid.Row="2" Margin="5">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <DataGrid  x:Name="dataGrid"
                    SelectionMode="Single"
                    AutoGenerateColumns="True"                      
                    Margin="0,5" Grid.Row="1" IsReadOnly="True"
                    ItemsSource="{Binding GridItem, Mode=TwoWay}"  SelectedItem="{Binding SelectedItem}"
                    AlternatingRowBackground="#FFF5F4F8" Background="White" Grid.ColumnSpan="2"
                    ScrollViewer.CanContentScroll="True" 
                    ScrollViewer.HorizontalScrollBarVisibility="Auto" 
                    ScrollViewer.VerticalScrollBarVisibility="Auto">
                </DataGrid>
            </Grid>

改成

<DataGrid Grid.Row="2"  x:Name="dataGrid"
                    SelectionMode="Single"
                    AutoGenerateColumns="True"                      
                    Margin="0,5" Grid.Row="1" IsReadOnly="True"
                    ItemsSource="{Binding GridItem, Mode=TwoWay}"  SelectedItem="{Binding SelectedItem}"
                    AlternatingRowBackground="#FFF5F4F8" Background="White" Grid.ColumnSpan="2"
                    ScrollViewer.CanContentScroll="True" 
                    ScrollViewer.HorizontalScrollBarVisibility="Auto" 
                    ScrollViewer.VerticalScrollBarVisibility="Auto">
                </DataGrid>
于 2012-05-11T19:06:30.480 回答
2

这里有几件事你应该记住分配宽度属性和数据网格视图。定义您的列,将最后一列的宽度设为自动,并分配一些 minWidth。

            <DataGrid  VerticalAlignment="Top" Margin="0,0,0,0" Height="221" HorizontalAlignment="Left" Width="234"
                DataContext="{DynamicResource ItemCollectionViewSource}" 
                        ItemsSource="{Binding}" AutoGenerateColumns="False" IsReadOnly="True" 
                        ScrollViewer.CanContentScroll="True" 
                        ScrollViewer.VerticalScrollBarVisibility="Auto"
                        ScrollViewer.HorizontalScrollBarVisibility="Auto" >
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding ID}" Header="ID"/>
                    <DataGridTextColumn Binding="{Binding URL}" Header="URL" Width="Auto" MinWidth="186"/>
                </DataGrid.Columns>
            </DataGrid>
于 2015-04-12T18:43:12.873 回答
1

我猜不出你做错了什么,因为你的代码的缩放最多的版本正在向我显示水平滚动条......

<UserControl.Resources>
    <x:ArrayExtension x:Key="MyArray" Type="{x:Type TextBlock}">
        <TextBlock Name="Test1" Text="12345678"
                   DataContext="12345678" Tag="12345689"/>
        <TextBlock Name="Test2" Text="12345678"
                   DataContext="12345678" Tag="12345689"/>
        <TextBlock Name="Test1" Text="12345678"
                   DataContext="12345678" Tag="12345689"/>
        <TextBlock Name="Test2" Text="12345678"
                   DataContext="12345678" Tag="12345689"/>
        <TextBlock Name="Test1" Text="12345678"
                   DataContext="12345678" Tag="12345689"/>
        <TextBlock Name="Test2" Text="12345678"
                   DataContext="12345678" Tag="12345689"/>
        <TextBlock Name="Test1" Text="12345678"
                   DataContext="12345678" Tag="12345689"/>
        <TextBlock Name="Test2" Text="12345678"
                   DataContext="12345678" Tag="12345689"/>
        <TextBlock Name="Test1" Text="12345678"
                   DataContext="12345678" Tag="12345689"/>
        <TextBlock Name="Test2" Text="12345678"
                   DataContext="12345678" Tag="12345689"/>
        <TextBlock Name="Test1" Text="12345678"
                   DataContext="12345678" Tag="12345689"/>
        <TextBlock Name="Test2" Text="12345678"
                   DataContext="12345678" Tag="12345689"/>            
    </x:ArrayExtension>
</UserControl.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <TextBlock HorizontalAlignment="Left"
               TextWrapping="Wrap"
               Text="My Text Clock"
               VerticalAlignment="Center" 
               Margin="10,10,0,10" FontSize="18.667"
               FontFamily="Segoe UI" />
    <Border Grid.Row="1" CornerRadius="2" BorderThickness="1">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="65"/>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Border BorderThickness="1" 
                 CornerRadius="2" Margin="5">
                <Grid Margin="0">
                       <ListBox
                           ItemsSource="{Binding
                                ViewMenuItems}"                            
                           VerticalAlignment="Center"
                           HorizontalAlignment="Left"/>             
    </Grid>
    </Border>
    <Grid Grid.Row="1" Margin="5">
             <StackPanel HorizontalAlignment="Left"
                         Orientation="Horizontal"
                         VerticalAlignment="Top" Margin="10,0,0,0">
                    <ContentControl Focusable="False"
                                    Content="ContentControl"  
                                    Height="16" Margin="0,5,0,0"/>
                     <TextBlock TextWrapping="Wrap"
                                Text="DisplayHerePlz!"
                                Margin="5,3,0,0"
                                VerticalAlignment="Center"/>
              </StackPanel>
     </Grid>
         <Grid Grid.Row="2" Margin="5">
            <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
             <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="*"/>
             </Grid.ColumnDefinitions>
    <tk:DataGrid x:Name="dataGrid"
            SelectionMode="Single"
       AutoGenerateColumns="True"   
               Margin="0,5" Grid.Row="1"
            IsReadOnly="True"   
            ItemsSource="{StaticResource MyArray}"
            ScrollViewer.CanContentScroll="True" 
            ScrollViewer.HorizontalScrollBarVisibility="Auto" 
            ScrollViewer.VerticalScrollBarVisibility="Auto"
       AlternatingRowBackground="#FFF5F4F8" 
            Background="White"
            Grid.ColumnSpan="2">                        
      </tk:DataGrid>
    </Grid>
   </Grid>
  </Border>
</Grid>

它可能是我省略的其他位,这可能导致水平宽度超出滚动视图......

于 2012-05-11T12:16:47.217 回答