c# - WPF数据绑定(bind)问题

标签 c# wpf data-binding grid tabcontrol

我在选项卡上的 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 像素)。

只需将 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>

此处的网格将是黑色的,并且将始终拉伸(stretch)到窗口的大小,使用 5 像素的边距,您将看到这是因为窗口的背景颜色是粉红色。

关于c# - WPF数据绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5953360/

相关文章:

c# - Func 与自定义委托(delegate)性能

c# - 带有滑入效果的WPF切换桌面

c# - 子文件夹中位图的 URI (c# wpf)

c# - 在 WPF 窗口及其用户控件之间共享数据的最佳方式是什么?

c# - 绑定(bind)到属性不起作用

c# - 调用 Application.Shutdown() 后,使用 ShowDialog 显示窗口不起作用

c# - 从集合绑定(bind)到 DataGridComboBoxColumn

android - 点击事件在MVVM架构中的位置

c# - 使用绑定(bind)更改 GridView 项的可见性

c# - 如何使用 app.config 阻止 TextWriterTraceListener 附加?