Getting the value of Height through OneWayToSource binding
-
I have a ListBox whose Height is bound one way to source. As you can see from the XAML below, the listbox is supposed to take up all the space available to it, and it's height is not initialised to anything in C# code.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ListBox ItemsSource="{Binding PhotoList}" Grid.Row="0" Height="{Binding Path=listBoxH, Mode=OneWayToSource}"></ListBox>
</Grid>
My problem is that I need to get a numerical value for
listBoxH
in the code behind (bounded code, it's actually the ViewModel of my UI). But this value isdouble.NaN
. This is expected as the Height/listBoxH
is not "set" anywhere, but is there a way to get the ActualHeight of the ListBox? I need to get the current height of the ListBox, use it do a calculation which decides what to display in the ListBox. I can't directly reference the ListBox in C# code as it doesn't belong to the same class, and this is not the approach I want to take. Workarounds, anyone? -
I have a ListBox whose Height is bound one way to source. As you can see from the XAML below, the listbox is supposed to take up all the space available to it, and it's height is not initialised to anything in C# code.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ListBox ItemsSource="{Binding PhotoList}" Grid.Row="0" Height="{Binding Path=listBoxH, Mode=OneWayToSource}"></ListBox>
</Grid>
My problem is that I need to get a numerical value for
listBoxH
in the code behind (bounded code, it's actually the ViewModel of my UI). But this value isdouble.NaN
. This is expected as the Height/listBoxH
is not "set" anywhere, but is there a way to get the ActualHeight of the ListBox? I need to get the current height of the ListBox, use it do a calculation which decides what to display in the ListBox. I can't directly reference the ListBox in C# code as it doesn't belong to the same class, and this is not the approach I want to take. Workarounds, anyone?Most (all?) controls that have a Height also have an ActualHeight property. Not sure if you can bind to it. You might be able to use a size change event to detect when the size has changed then then get the value of ActualHeight.
-
Most (all?) controls that have a Height also have an ActualHeight property. Not sure if you can bind to it. You might be able to use a size change event to detect when the size has changed then then get the value of ActualHeight.
Thanks for the tip. I found that changes to ActualHeight could be used as a trigger. I've tried to use an event trigger, and am currently stuck. Here's what I've done:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="{Binding gridHeight}" />
</Grid.RowDefinitions>
<ListBox ItemsSource="{Binding PhotoList}" Grid.Row="0" >
<ListBox.Style>
<Style>
<Style.Triggers>
<EventTrigger RoutedEvent="SizeChanged">
<!-- Set {Binding tabHeight} to ActualHeight here -->
</EventTrigger>
</Style.Triggers>
</Style>
</ListBox.Style>
</ListBox>
</Grid>The problem is, I can't figure out how to tell it to update
tabHeight
once the event is triggered. I initially thought I could use aSetter
to update{Binding Path=tabHeight}
. But that doesn't seem to be the case. Suggestions are much appreciated :)