2011-05-10 287 views
2

我在选项卡上的画布内部有一个网格。 该网格包含一个大的位图图像, 我已经(试图)将网格的大小限制为该选项卡的大小,并且在网格周围还有一个5像素的边距。WPF数据绑定问题

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

+1

如果你想要网格填充选项卡,那么你为什么把它放在一个画布?画布适用于需要以精确尺寸(X,Y)坐标精确定位控件的情况 - 对于“填充父项”等动态布局来说,这没有好处。 – 2011-05-10 18:10:24

回答

1

根本不需要设置高度属性(同时也意识到,如果您考虑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像素保证金,你会看到,因为窗户的背景颜色是粉红色的。