将位置绑定到 ActualHeight

发布于 2024-09-01 14:29:31 字数 768 浏览 4 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不弃不离 2024-09-08 14:29:31

尝试绑定 {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'}

从﹋此江山别 2024-09-08 14:29:31

在我看来,您在这项工作中使用了错误的控件。 Grid 可以轻松处理此问题:-

 <Grid>

    <Canvas x:Name="DisplaySurface">
    </Canvas>
    <ListBox HorizontalAlignment="Left" VerticalAlignment="Bottom" ...>
       <!-- items --->
    </ListBox>
</Grid> 

现在 ListBox 始终出现在左下角。不仅如此,如果总可用高度小于列表框中所有内容的高度,它将被限制在可用高度并显示滚动条。否则你的代码将不得不经历一些困难才能解决。

Sounds to me like you are using the wrong control for the job. A Grid can handle this without all this effort:-

 <Grid>

    <Canvas x:Name="DisplaySurface">
    </Canvas>
    <ListBox HorizontalAlignment="Left" VerticalAlignment="Bottom" ...>
       <!-- items --->
    </ListBox>
</Grid> 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文