我在选项卡上的 Canvas 内有一个网格。 网格包含一个大的位图图像, 我已经(尝试)将网格的大小绑定(bind)到选项卡的大小,并且在网格周围还有五个像素的边距。
imageTab.cs
public ImageTab(SendInfo sendInfo, int numImge, int numAccs)
{
imageDisplay = new ImageDisplay(sendInfo, numImge, numAccs);
imageDisplay.ClipToBounds = true;
CreateCanvas();
}
private void CreateCanvas()
{
Canvas canvas = new Canvas();
canvas.Children.Add(imageDisplay);
this.AddChild(canvas);
}
ImageDisplay.xaml
<UserControl x:Class="MyProj.ImageDisplay">
<Grid Margin="5,5,5,5" Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=TabControl, AncestorLevel=1}, Path=ActualHeight}">
<Image/>
</Grid>
</UserControl>
网格从选项卡区域的底部稍微脱落,导致图像底部被切除。 我的数据绑定(bind)有问题吗,我需要对其应用某种偏移量吗? (选项卡的大小 - 边距为 10 像素?)
最佳答案
您根本不需要设置 Height 属性(也意识到这样做是不正确的,因为当您考虑 5 像素边距时,即它会偏离 10 像素)。
只需将 VerticalAlignment
和 HorizontalAlignment
保留为默认值(即 Stretch
)即可获得您想要的效果。
在新的 Window
上试试看我的意思:
<Window x:Class="WpfApplication9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="438" Width="587" Background="Pink">
<Grid Background="Black" Margin="5">
</Grid>
</Window>
此处的网格将是黑色的,并且将始终拉伸(stretch)到窗口的大小,使用 5 像素的边距,您将看到这是因为窗口的背景颜色是粉红色。
关于c# - WPF数据绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5953360/