2011-05-10 99 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像素保證金,你會看到,因爲窗戶的背景顏色是粉紅色的。