Wpf 在带有图像的列表框中将宽度绑定到ViewportWidth时,水平滚动条可见
我试图实现的是创建一个垂直的照片列表,其中帧位于WPF窗口的最右侧。图像是绑定到ObservableCollection的数据,用户应该能够使用GridSplitter调整图像列表的大小 目前的代码如下:Wpf 在带有图像的列表框中将宽度绑定到ViewportWidth时,水平滚动条可见,wpf,listbox,scrollbar,border,Wpf,Listbox,Scrollbar,Border,我试图实现的是创建一个垂直的照片列表,其中帧位于WPF窗口的最右侧。图像是绑定到ObservableCollection的数据,用户应该能够使用GridSplitter调整图像列表的大小 目前的代码如下: <ListBox Grid.Row="0" Grid.Column="2" Grid.RowSpan="3" ItemsSource="{Binding Sheet.Images}"> <ListBox.ItemTemplate>
<ListBox Grid.Row="0" Grid.Column="2" Grid.RowSpan="3" ItemsSource="{Binding Sheet.Images}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Background="#CCC" BorderThickness="8" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ScrollViewer}}, Path=ViewportWidth}">
<Image Source="{Binding Contents}"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<GridSplitter Grid.Row="0" Grid.Column="1" Grid.RowSpan="3" Width="2" Background="Transparent"/>
它有一个问题——不管图像数量多少,水平滚动条总是可见的,而右边框的部分是隐藏的,除非通过滚动来发现。
当我绑定到ActualWidth时,问题更加明显,因为滚动条宽度没有从父容器宽度中减去
如何创建这样一个垂直的图像列表,垂直滚动条在需要时可见,而水平滚动条在看到整个边框时不可见
还有一个小问题:如何在两幅连续图像的边界之间增加距离?我找到了解决这个问题的方法,尽管不是很优雅。 由于某种原因,ViewportWidth似乎比报告的宽度窄2个像素(或者图像边框宽2个像素)。无论如何,我使用了MathConverter,可以找到它 最终结果是:
<ListBox Grid.Row="0" Grid.Column="2" Grid.RowSpan="3" ItemsSource="{Binding Sheet.Images}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Background="#CCC" BorderThickness="8" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ScrollViewer}},
Path=ViewportWidth,
Converter={converters:MathConverter},
ConverterParameter=@VALUE-2}">
<Image Source="{Binding Contents}"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<GridSplitter Grid.Row="0" Grid.Column="1" Grid.RowSpan="3" Width="2" Background="Transparent"/>
如果有人有更好的解决方案,或者至少有理由解释为什么这两个像素首先会导致问题,请随意分享