WPF TabItem 网格内容中途消失
我有一个奇怪的问题。我有一个像这样创建的 TabControl:
<TabControl HorizontalAlignment="Stretch" Name="tabControl" VerticalAlignment="Stretch" FontSize="10">
...
</TanControl>
像这样创建了几个选项卡:
<TabItem Header="XXXX" Name="tab1">
...
</TabItem>
在每个选项卡内我都有一个网格,按照工具箱拖放生成的代码,我开始向第一个控件添加数据,如下所示:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="XXXX:" Margin="1,6" Height="28" HorizontalAlignment="Right" Name="label001" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,34" Height="28" HorizontalAlignment="Right" Name="label002" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,62" Height="28" HorizontalAlignment="Right" Name="label003" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,90" Height="28" HorizontalAlignment="Right" Name="label004" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,130" Height="28" HorizontalAlignment="Right" Name="label005" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,158" Height="28" HorizontalAlignment="Right" Name="label006" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,186" Height="28" HorizontalAlignment="Right" Name="label007" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,226" Height="28" HorizontalAlignment="Right" Name="label008" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,254" Height="28" HorizontalAlignment="Right" Name="label009" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="1" Content="XXXX" Margin="1,254" Height="28" HorizontalAlignment="Left" Name="lbl1" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,282" Height="28" HorizontalAlignment="Right" Name="label010" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="1" Content="XXXX" Margin="1,282" Height="28" HorizontalAlignment="Left" Name="lbl2" VerticalAlignment="Top" FontSize="12" />
</Grid>
问题是,当标签放置在网格中低于中间位置时,它就会消失。如果我将窗口大小调整得更大,我可以看到更多项目,因为它们不再超过一半,如果我将窗口变小,那么更多项目就会消失,因为它们超过了中间点。
我尝试将行添加到网格中,但它们仍然消失!奇怪的是,如果我添加 n 行并将标签在它们之间分开,每行都会在每行的中间点被切断。
这个问题也会出现在设计视图中,但前提是施加垂直限制,例如行高或窗口最大高度(因为设计视图会在我添加容器时拉伸容器以适合所有内容)。
最后,无论柱子变得多小,它们都不会中途消失。
I have a strange problem. I have a TabControl created like so:
<TabControl HorizontalAlignment="Stretch" Name="tabControl" VerticalAlignment="Stretch" FontSize="10">
...
</TanControl>
with a few tabs created like so:
<TabItem Header="XXXX" Name="tab1">
...
</TabItem>
Inside each tab I have a grid, following the code generated by the toolbox drag and drop, I started adding data to the first control like so:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="XXXX:" Margin="1,6" Height="28" HorizontalAlignment="Right" Name="label001" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,34" Height="28" HorizontalAlignment="Right" Name="label002" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,62" Height="28" HorizontalAlignment="Right" Name="label003" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,90" Height="28" HorizontalAlignment="Right" Name="label004" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,130" Height="28" HorizontalAlignment="Right" Name="label005" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,158" Height="28" HorizontalAlignment="Right" Name="label006" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,186" Height="28" HorizontalAlignment="Right" Name="label007" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,226" Height="28" HorizontalAlignment="Right" Name="label008" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,254" Height="28" HorizontalAlignment="Right" Name="label009" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="1" Content="XXXX" Margin="1,254" Height="28" HorizontalAlignment="Left" Name="lbl1" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="0" Content="XXXX:" Margin="1,282" Height="28" HorizontalAlignment="Right" Name="label010" VerticalAlignment="Top" FontSize="12" />
<Label Grid.Column="1" Content="XXXX" Margin="1,282" Height="28" HorizontalAlignment="Left" Name="lbl2" VerticalAlignment="Top" FontSize="12" />
</Grid>
The problem is, after a label is placed farther down than halfway in the grid, it disappears. If I resize the window to be bigger, I can see more items because they are no longer past halfway, if I make it smaller then more disappear because they pass the halfway point.
I have tried adding rows to the grid and they still disappear! The strange part is if I add n rows and split the labels up between them, each row gets cut off at the halfway point in each row.
This problem also shows in the design view, but only if a vertical limit is imposed like row height or window max height (because the design view stretches the containers to fit all the contents as I add them).
And finally, the columns never disappear halfway no matter how small they get.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,最后一个标签的边距“1,282”与“1,282,1,282”(左、上、右、下)相同。解决问题的简单方法是将每个标签的边距修改为“X,X,1,0”。
您正在使用 Margin 来控制每个控件的位置,但我建议您使用 WPF 布局控件的附加属性来控制位置。例如,Gird 有 Gird.Row 和 Grid.Column,DockPannel 有 Dock.Top 和 Dock.Left 等。关于这个问题,请参考 使用自动布局概述。
So, the margin "1,282" of the last label is the same with "1,282,1,282"(left,top,right,bottom). The simple way to slove your problem is to modify the margin of every label to "X,X,1,0".
You are using Margin to control a position of each control but I recommend you to use attached properties of Layout Controls of WPF to control a position. For example, for Gird there are Gird.Row and Grid.Column and for DockPannel there are Dock.Top and Dock.Left etc. For this issue, please refer to Use Automatic Layout Overview.