将位置绑定到 ActualHeight
我想在 XAML 中将列表位置绑定到它自己的高度。所以它的左下角始终位于画布的 0.0 处。我使用 elementBinding 获取 ActualHeight 并使用转换器来反转属性。但发送到转换器的高度是 0。
我该如何解决这个问题,或者我是否以错误的方式处理这个问题?
<Canvas x:Name="DisplaySurface">
<ListBox x:Name="MenuList" Visibility="Visible"
Canvas.Top="{Binding ElementName=MenuList, Path=ActualHeight,
Converter={StaticResource LamdaConv}, ConverterParameter='val=>-val'}">
<ListBoxItem Content="item 1" />
<ListBoxItem Content="item 2" />
<ListBoxItem Content="item 3" />
<ListBoxItem Content="item 4" />
<ListBoxItem Content="item 5" />
<ListBoxItem Content="item 6" />
</ListBox>
</Canvas>
I want to bind a lists position to its own height in XAML. So its lower left corner always will be at 0.0 of the canvas. I'm using elementBinding to get the ActualHeight
and a converter to invert the property. But the height sent to the converter is 0.
How do I solve this or am I going at this the wrong way?
<Canvas x:Name="DisplaySurface">
<ListBox x:Name="MenuList" Visibility="Visible"
Canvas.Top="{Binding ElementName=MenuList, Path=ActualHeight,
Converter={StaticResource LamdaConv}, ConverterParameter='val=>-val'}">
<ListBoxItem Content="item 1" />
<ListBoxItem Content="item 2" />
<ListBoxItem Content="item 3" />
<ListBoxItem Content="item 4" />
<ListBoxItem Content="item 5" />
<ListBoxItem Content="item 6" />
</ListBox>
</Canvas>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试绑定
{Binding ActualHeight,relativeSource={RelativeSource Self},Converter={StaticResource LamdaConv}, ConverterParameter='val=>-val'}
Try the binding
{Binding ActualHeight, RelativeSource={RelativeSource Self},Converter={StaticResource LamdaConv}, ConverterParameter='val=>-val'}
在我看来,您在这项工作中使用了错误的控件。
Grid
可以轻松处理此问题:-现在
ListBox
始终出现在左下角。不仅如此,如果总可用高度小于列表框中所有内容的高度,它将被限制在可用高度并显示滚动条。否则你的代码将不得不经历一些困难才能解决。Sounds to me like you are using the wrong control for the job. A
Grid
can handle this without all this effort:-Now the
ListBox
always appears in the bottom left corner. Not only that but if the total available height is less than the height of all the content in the list box it will be capped at the available height and show scroll bar. Something your code would otherwise have to jump through hoops working out.