Binding position to ActualHeight
-
27-09-2019 - |
Question
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>
Solution
Try the binding {Binding ActualHeight, RelativeSource={RelativeSource Self},Converter={StaticResource LamdaConv}, ConverterParameter='val=>-val'}
OTHER TIPS
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.