WPF:列表框会缩小,但其容器(DockPanel)不会 [英] WPF: Listbox shrinks but its container (DockPanel) doesn't

查看:155
本文介绍了WPF:列表框会缩小,但其容器(DockPanel)不会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在将TextBox与TextWrapping = Wrap一起使用时出现问题,它位于DataTemplate内,而DataTemplate则位于StackPanel内的ListBox内.

问题是:当我在TextBox中添加新行时,ListBox和StackPanel都相应地展开,但是当我从TextBox中删除行时,ListBox会收缩,但是容器StackPanel不会收缩(它保持相同的高度) ).也就是说,TextBox的高度导致DockPanel仅增长.

Hi
I have a problem when using a TextBox with TextWrapping=Wrap which is inside a DataTemplate which is inside a ListBox which is inside a StackPanel.

The problem is: when I add new lines to the TextBox, both the ListBox and the StackPanel expand accordingly, but when I remove lines from the TextBox, the ListBox shrinks, but the container StackPanel doesn''t shrink (it keeps the same height). That is, the TextBox height causes the DockPanel only to grow.

<pre lang="cs"><StackPanel>
   <ListBox>
      <ListBox.Resources>
         <DataTemplate
            <TextBox AcceptsReturn="True" TextWrapping="Wrap" Text="{Binding Path=Text, Mode=TwoWay}" Text="{Binding Path=SomeProperty, Mode=TwoWay}" />
         </DataTemplate>
      </ListBox.Resources>
   </ListBox>
</StackPanel>




这似乎是一个ListBox/DataTemplate问题.这是一个已知的错误吗?有解决方法吗? (请说是" ...)




This seems to be a ListBox/DataTemplate problem. Is it a known bug? Does it have a workaround? (please say "yes"...)

Thanks in advance!

推荐答案

似乎是,一旦用容器渲染了窗口,我们就需要再次渲染它.
解决方法是将滚动条设置为文本框

< TextBox MinWidth ="100" MinHeight ="45" MaxWidth ="100" MaxHeight ="45"
AcceptsReturn ="True" TextWrapping =包装"
ScrollViewer.CanContentScroll ="True"
ScrollViewer.Horizo​​ntalScrollBarVisibility ="Auto"
ScrollViewer.VerticalScrollBarVisibility ="Auto"/>
It seems to be, once the window is rendered with the container then we need to again render it.

The work around for this would be setting the scroll bar to the textbox

<TextBox MinWidth="100" MinHeight="45" MaxWidth="100" MaxHeight="45"
AcceptsReturn="True" TextWrapping="Wrap"
ScrollViewer.CanContentScroll="True"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"/>


这篇关于WPF:列表框会缩小,但其容器(DockPanel)不会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆