2014-03-05 76 views
2

需要帮助使用xaml布局。在WrapPanel内部自动调整大小?

我有一个内部有两个元素的Wrapanel。 MainGrid的左边一个需要固定在900像素。第二个是AuxillaryDataScrollViewer,如果WrapPanel包装它,我希望最小为650,最大为100%。这可能吗?

这是我走到这一步:

<Window x:Class="scratch.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="768" Width="1024"> 
    <Grid> 
     <ScrollViewer> 
      <WrapPanel> 
       <Grid Width="900" Height="800" Background="Bisque"></Grid> 
       <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Hidden" Width="650"> 
        <Grid Width="1500" Height="700" Background="Tomato"></Grid> 
       </ScrollViewer> 
      </WrapPanel> 
     </ScrollViewer> 
    </Grid> 
</Window> 

谢谢!

编辑加入了更多的细节:

客户希望做数据录入,看到在面板右侧的计算结果实时,但一些在他的实验室的电脑只能够1280pixels对那些宽度,他希望将结果包装到数据输入表单下方。

+0

你想要两个元素并排吗? – voddy

+0

如果显示器有分辨率,如果不是我想要它包装。 – safetyOtter

+0

我测试了你的代码,它可以像你期望的那样工作。 – voddy

回答

3

由于要求如此明确,您可以简单地添加一个SizeChanged处理程序,并根据容器的宽度手动设置第二个元素的宽度。如果容器小于900 + 650,则将第二个元件拉伸至容器的100%。

public MainWindow() 
{ 
    SizeChanged += MainWindow_SizeChanged; 
} 

void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e) 
{ 
    double firstElementWidth = MainGrid.Width;       // 900 
    double secondElementMinWidth = AuxillaryDataScrollViewer.MinWidth; // 650 

    double secondElementWidth; 

    // if wrapped, stretch to the container 
    if (e.NewSize.Width < firstElementWidth + secondElementMinWidth) 
     secondElementWidth = e.NewSize.Width; 
    // otherwise (not wrapped), fill the remainder of the first row 
    else 
     secondElementWidth = e.NewSize.Width - firstElementWidth; 
} 

很明显,这是快速和肮脏的。如果你需要更强大的东西,那么我建议你写一个custom panel,它符合你需要的约定。 WrapPanel无法将元素拉伸到整体宽度,但没有理由不能设计出可以做到的面板。

+0

我想到了这一点,但不希望在代码审查期间因缺少更简单/更少的kludgey解决方案而被人嘲笑。谢谢! – safetyOtter