[英]Horizontal Scrolling Issue
I'm new to WPF and I'm trying to create an dashboard interface for an app I'm developing. 我是WPF的新手,正在尝试为我正在开发的应用程序创建仪表板界面。 My problem is as follows;
我的问题如下;
I have a datagrid which will contain my data. 我有一个数据网格,其中将包含我的数据。 This datagrid is inside a grid because I needed superheaders over particular sections.
该数据网格位于网格内部,因为我需要在特定部分上使用超级头。 I inserted a test row into the data table and it shows up fine, but the datagrid element inside the grid seems to have its own scrollbar.
我在数据表中插入了一个测试行,它显示得很好,但是网格内的datagrid元素似乎具有自己的滚动条。 Both the super header row and the datagrid row extend off the window but only the datagrid row scrolls.
超级头行和数据网格行都从窗口伸出,但是只有数据网格行滚动。 The 2 visible super headers stay in the same place while the datagrid row scrolls.
当datagrid行滚动时,2个可见的超级标题保留在同一位置。 Below is my xaml code to generate the interface.
下面是我的xaml代码来生成接口。 I have tried wrapping the grid in a scrollviewer and also setting the horizontal scrollbar of the row with the super headers to visible but nothing has worked so far.
我试过将网格包装在scrollviewer中,还将带有超级标题的行的水平滚动条设置为可见,但到目前为止没有任何效果。 If I can get the super headers to scroll at all at least I can try to make an event handler to move the super headers when the datagrid scrollbar is moved.
如果我至少可以滚动超级标题,则可以尝试使事件处理程序在移动datagrid滚动条时移动超级标题。 Sorry if this seems kinda garbled but I haven't had my coffee yet and I also have a migraine, 2 of the best things for a programmer to experience.
抱歉,这似乎有点乱码,但是我还没有喝咖啡,而且我还有偏头痛,这是程序员可以体验的2件事。 Thanks in advance
提前致谢
<Canvas HorizontalAlignment="Center" Height="508" VerticalAlignment="Top" Width="1366" Margin="-4,4,2,-7">
<Grid Canvas.Top="2">
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" ScrollViewer.CanContentScroll="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding ElementName=grid, Path=RowHeaderWidth}" />
<ColumnDefinition Width="{Binding ElementName=TypeCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=NCRCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=StatusCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=PStkCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=DfStkCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=QtyCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=DfCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=DfDateCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=b1, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=CCodeCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=ByCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=InvAssignCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=InvCommentsCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=b2, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=CStepNoCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=CActionCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=CAssignCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=CReqCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=CComplCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=MCostCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=LabourCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=b3, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=PStepCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=PActionCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=PAssignedCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=PReqCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=PCompCol, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=PDateCol, Path=ActualWidth}" />
</Grid.ColumnDefinitions>
<TextBlock Text="INVESTIGATION" Height="20" Grid.Column="10" Grid.ColumnSpan="5" Grid.Row="0"/>
<TextBlock Text="CORRECTIVE ACTION" Height="20" Grid.Column="15" Grid.ColumnSpan="8" Grid.Row="0" />
<TextBlock Text="PREVENTATIVE ACTION" Height="20" Grid.Column="23" Grid.ColumnSpan="6" Grid.Row="0"/>
</Grid>
<DataGrid x:Name="grid" Grid.Row="1" Grid.Column="0" CanUserAddRows="False" HorizontalAlignment="Center" VerticalAlignment="Top" Width="1524" Canvas.Top="44" HeadersVisibility="Column" AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="False" Canvas.Left="-180" CellEditEnding="grid_CurrentCellChanged" Margin="0,0,881,0">
<DataGrid.Columns>
<DataGridTextColumn x:Name="TypeCol" Binding="{Binding Type}" Header="Type" Width="50" IsReadOnly="True"/>
<DataGridTextColumn x:Name="NCRCol" Binding="{Binding NCR}" Header="NCR" Width="100" IsReadOnly="True"/>
<DataGridTextColumn x:Name="StatusCol" Binding="{Binding Status}" Header="Status" Width="50"/>
<DataGridTextColumn x:Name="PStkCol" Binding="{Binding PStkCode}" Header="Parent Stock Code" Width="120 " IsReadOnly="True"/>
<DataGridTextColumn x:Name="DfStkCol" Binding="{Binding DfStkCode}" Header="Defective Stock Code" Width="125" IsReadOnly="True"/>
<DataGridTextColumn x:Name="QtyCol" Binding="{Binding Qty}" Header="Quantity" Width="120" IsReadOnly="True"/>
<DataGridTextColumn x:Name="DfCol" Binding="{Binding Defect}" Header="Defect" Width="100" IsReadOnly="True"/>
<DataGridTextColumn x:Name="DfDateCol" Binding="{Binding Defect Date}" Header="Defect Date" Width="100" IsReadOnly="True"/>
<DataGridTemplateColumn x:Name="b1" Width="20" IsReadOnly="True"/>
<DataGridTextColumn x:Name="CCodeCol" Header ="Cause Code" Width ="80" />
<DataGridTextColumn x:Name="ByCol" Header ="By" Width ="80"/>
<DataGridTextColumn x:Name="InvAssignCol" Header ="Assigned To" Width ="100" />
<DataGridTextColumn x:Name="InvCommentsCol" Header ="Comments" Width ="100" />
<DataGridTemplateColumn x:Name="b2" Width="20" IsReadOnly="True"/>
<DataGridTextColumn x:Name="CStepNoCol" Header ="Step No" Width ="60" />
<DataGridTextColumn x:Name="CActionCol" Header ="Action" Width ="100" />
<DataGridTextColumn x:Name="CAssignCol" Header ="Assigned To" Width ="100"/>
<DataGridTextColumn x:Name="CReqCol" Header ="Reqd By" Width ="100" />
<DataGridTextColumn x:Name="CComplCol" Header ="Completed By" Width ="100"/>
<DataGridTextColumn x:Name="MCostCol" Header="Material Cost($)" Width="100"/>
<DataGridTextColumn x:Name="LabourCol" Header="Labour (Hrs)" Width="100"/>
<DataGridTemplateColumn x:Name="b3" Width="20" IsReadOnly="True"/>
<DataGridTextColumn x:Name="PStepCol" Header="Step No" Width="60"/>
<DataGridTextColumn x:Name="PActionCol" Header="Action" Width="100"/>
<DataGridTextColumn x:Name="PAssignedCol" Header="Assigned To" Width="100"/>
<DataGridTextColumn x:Name="PReqCol" Header="Reqd By" Width="100"/>
<DataGridTextColumn x:Name="PCompCol" Header="Completed By" Width="100"/>
<DataGridTextColumn x:Name="PDateCol" Header="Completed Date" Width="100"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Canvas>
Rather than trying to match the columns and ScrollBar
s of the DataGrid
in a Grid
that has no relation to it, just define the DataGrid
header however you want to. 与其尝试与不相关的
Grid
中的DataGrid
的列和ScrollBar
匹配,不如定义自己想要的DataGrid
标头。 You can use the HeaderTemplate
property of a DataGrid.DataGridTemplateColumn
... maybe something like this: 您可以使用
DataGrid.DataGridTemplateColumn
的HeaderTemplate
属性...也许像这样:
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Top Header" />
<TextBlock Grid.Row="1" Text="Bottom Header" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.