WPF数据绑定问题

发布于 2024-11-06 19:19:49 字数 903 浏览 0 评论 0原文

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

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 像素?)

I have a grid inside a canvas on a tab.
The grid contains a large bitmap image,
I have(tried to) bound the size of the grid to the size of the tab and also have a five pixel margin around the grid.

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>

The grid comes off the bottom of the tab area slightly causing the bottom of the image to be cut off.
Is there a problem with my databinding, do I need to apply some sort of offset to it? (size of tab - 10pixels for the margin?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎夕旧梦 2024-11-13 19:19:49

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

只需将 VerticalAlignmentHorizo​​ntalAlignment 保留为默认值(即 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 像素的边距,您将看到这是因为窗口的背景颜色是粉红色的。

You don't need to set the Height property at all (also realize that it is incorrect to do so as you have it when you consider the 5 pixel margin, i.e., it would be off by 10 pixels).

Just leave VerticalAlignment and HorizontalAlignment at their default values (which is Stretch) to get the effect you are after here.

Try this on a new Window to see what I mean:

<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>

The grid here will be black and will always stretch to the size of the window, using a 5 pixel margin which you will see because the Window's back color is pink.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文