2012-07-21 45 views
2

我在一個堆棧面板中有5個邊框,每個邊框的寬度都是窗口寬度/ 5。當我最大化窗口時,每個邊框寬度應根據窗口寬度/ 5調整大小。基於窗口寬度調整控件大小

我已經嘗試過使用轉換器,但它不能像轉換器知道窗口已經調整大小那樣工作。

<Window x:Class="ItemPanelTemplateTest.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <StackPanel Orientation="Horizontal"> 
     <Border Height="20" Background="Red" Width="105" /> 
     <Border Height="20" Background="Green" Width="105" /> 
     <Border Height="20" Background="Yellow" Width="105" /> 
     <Border Height="20" Background="Blue" Width="105" /> 
     <Border Height="20" Background="Orange" Width="105" /> 
    </StackPanel> 
</Window> 

我不想在codebehind上寫任何東西,因爲我使用的是MVVM。

回答

3

使用與StackPanel不同的容器。這裏最好的候選人GridUniformGrid,但因爲後者需要較少的打字,那就是:

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <UniformGrid Height="20" Rows="1"> 
     <Border Background="Red" /> 
     <Border Background="Green" /> 
     <Border Background="Yellow" /> 
     <Border Background="Blue" /> 
     <Border Background="Orange" /> 
    </UniformGrid> 
</Window> 

該網格將與窗口自動調整,然後均勻地調整其內容。

相關問題