Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何在没有垂直滚动条的情况下获取或计算列表框的实际宽度(如果可见)_Wpf_Binding_Listbox_Scrollbar - Fatal编程技术网

Wpf 如何在没有垂直滚动条的情况下获取或计算列表框的实际宽度(如果可见)

Wpf 如何在没有垂直滚动条的情况下获取或计算列表框的实际宽度(如果可见),wpf,binding,listbox,scrollbar,Wpf,Binding,Listbox,Scrollbar,我想知道如果没有垂直滚动条(如果可见),如何获取或计算ListBox的实际宽度 我想做的是改变列表框中每个项目的宽度,而不被垂直滚动条覆盖 至少,上面的绑定告诉我ListBox的实际宽度,但这个绑定不处理垂直滚动条的宽度 有什么好方法可以解决这个问题吗?尝试绑定到containingScrollViewer的ViewportWidth属性 像这样: Width="{Binding Path=ViewportWidth, RelativeSource={RelativeSource Mode=Fi

我想知道如果没有垂直滚动条(如果可见),如何获取或计算ListBox的实际宽度

我想做的是改变列表框中每个项目的宽度,而不被垂直滚动条覆盖

至少,上面的绑定告诉我ListBox的实际宽度,但这个绑定不处理垂直滚动条的宽度


有什么好方法可以解决这个问题吗?

尝试绑定到containing
ScrollViewer
ViewportWidth
属性

像这样:

Width="{Binding Path=ViewportWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ScrollViewer}}"

也许默认设置会对您有所帮助:只是根本不设置
Width

至少在我的例子中,不设置
Width
属性就足够了。虽然Mårten WikströM的回答完全正确,但我发现我的实际问题是我指定了(不必要的)
Width
,并以错误的方式这样做(与Aki24x报告的方式相同)

这是我的示例的一个简化案例。解决方案是删除第4行

1  <ListBox.ItemTemplate>
2      <DataTemplate>
3          <Border
4              Width="..." 
5              >
1
2.
3.

请注意,这将绑定到包含ScrollViewer的ViewportWidth,而不是内部的。虽然您可以在ScrollViewer中包装列表框而不会产生不良后果,但如果您使用TreeView进行包装,它似乎会禁用UI虚拟化,这可能会导致许多项目出现性能问题。
1  <ListBox.ItemTemplate>
2      <DataTemplate>
3          <Border
4              Width="..." 
5              >