【问题标题】:WPF Height Databinding questionWPF高度数据绑定问题
【发布时间】:2009-11-02 17:05:19
【问题描述】:

我有一个 ScrollViewer,我正在尝试对其高度进行数据绑定。 ScrollViewer 拥有一个长列表框

所以这是我的问题。我的 ScrollViewer 将绑定到我的窗口的高度 (Name="MainForm") 就好了。但是太长了。

如果我尝试绑定到窗口中的网格 (Name="MainGrid"),则 ScrollViewer 会展开到列表框的全长(不滚动)。

显然我可以对高度进行硬编码,但它不会随窗口调整大小。一个我不想放弃的功能。

有什么想法吗?

这是绑定设置为 MainGrid 的 xaml

<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WI Assistant" Height="621.25" Width="943.75" Name="MainForm" FontSize="14">
<Grid Name="MainGrid">
    <DockPanel Margin="266.25,0,455,12" HorizontalAlignment="Left" Name="dockPanel1">
        <StackPanel>
           <ScrollViewer  Height="{Binding ElementName=MainGrid, Path=Height}">
                    <ListBox Name="cboProjects"  FontSize="14" >
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
               </ListBox>
           </ScrollViewer>
        </StackPanel>
    </DockPanel>
</Grid>

(这将在XamlPadx 中运行 (F5))

【问题讨论】:

    标签: wpf data-binding xaml


    【解决方案1】:

    将其从StackPanel 中取出,因为StackPanels 在其方向上不受限制地增长(在本例中为垂直方向)。从最简单的解决方案开始:

    <Window>
        <ScrollViewer>
            <ItemsControl>
                ...
            </ItemsControl>
        </ScrollViewer>
    </Window>
    

    然后根据需要添加复杂性。

    【讨论】:

    • +1 将StackPanel 与单个元素一起使用往往会导致这些问题。
    • 祝福你!我正要把头发拉出来。顺便说一句,我正在使用堆栈面板来堆叠扩展器(超过 1 个控件)。我在发布之前简化了我的 Xaml。现在我知道我不能在堆栈面板内的扩展器内使用 ListBox。谢谢!
    • 或者实际上是 StackPanel 中的 ListBox(除了扩展器)
    • +1 Stackpanels 中的 Scrollviewers 通常不起作用,我也可能因此而扯掉头发
    • 哎呀。我稍微改变了我的答案,在ScrollViewer 中有一个ItemsControl,而不是ListBox。那是因为ListBox的模板已经有了ScrollViewer,所以外部的ScrollViewer是没用的。
    猜你喜欢
    • 2011-10-18
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多