【问题标题】:WPF databinding problemWPF数据绑定问题
【发布时间】:2011-05-10 16:36:43
【问题描述】:

我在选项卡上的画布内有一个网格。 网格包含一个大的位图图像, 我已经(试图)将网格的大小绑定到选项卡的大小,并且网格周围还有五个像素的边距。

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>

网格从选项卡区域的底部略微脱落,导致图像底部被截断。 我的数据绑定有问题,我需要对其应用某种偏移量吗? (标签大小 - 10 像素的边距?)

【问题讨论】:

  • 如果你想让网格填充标签,那你为什么要把它放在 Canvas 里面呢?画布适用于您需要将控件定位在具有精确尺寸的精确 (X,Y) 坐标上的情况——它不适合“填充父级”等动态布局。

标签: c# wpf data-binding grid tabcontrol


【解决方案1】:

您根本不需要设置 Height 属性(也意识到这样做是不正确的,因为当您考虑 5 个像素的边距时,它会偏离 10 个像素)。

只需将VerticalAlignmentHorizontalAlignment 保留为默认值(即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>

这里的网格将是黑色的,并且总是会拉伸到窗口的大小,使用 5 像素的边距,您会看到这是因为窗口的背景颜色是粉红色的。

【讨论】:

    猜你喜欢
    • 2011-10-18
    • 2014-05-25
    • 1970-01-01
    • 2010-11-11
    相关资源
    最近更新 更多