1

I am trying to find a way to access the window height of my screen from my view model, so that I can set my MaxHeight to be a percentage of the window height.

XAML

<UserControl>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SizeChanged">
            <i:InvokeCommandAction Command="{Binding SizeChangedCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
<Grid>
    <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
    <Grid.RowDefinitions/>
    <ListBox Grid.Row="0"
             Content={Binding SomeContent}
             MaxHeightMaxHeight="{Binding PercentageMaxHeight}"/>
    <Button Content="ClickMe"
            Grid.Row="1" />
</Grid>
<UserControl/>

To be clear, this is not my actual complete XAML. The idea is that I want my ListBox to have a maximum height that is a percentage of the screen size so that my button is always right below my listbox, but the listbox does not push the button off the screen when it's too filled up.

View Model

public PercentageMaxHeight;
public ICommand SizeChangedCommand => new RelayCommand(SizeChangedCommandExecuted);

private void SizeChangedCommandExecuted()
{
    /*I am looking for a way to access ActualHeight, or any other way of getting the height of my window 
    here, so I can update PercentageMaxHeight. */
}

I don't want to set it from the code behind my XAML, because I am trying to follow MVVM.

prelude38
  • 107
  • 7
  • _"so that my button is always right below my listbox, but the listbox does not push the button off the screen when it's too filled up"_ -- I still don't understand your question. If you use `"*"` instead of `"Auto"` for the row height of the `ListBox` row, it will do exactly what you want. The `ListBox` row will be expanded to fill whatever's left after accounting for all the non-`"*"` rows (in this case, just the one for `Button`). That seems to be exactly what you're asking for. Why isn't it? What is different about that and what you actually want? – Peter Duniho Mar 27 '20 at 23:32
  • You can't use * for max height. – prelude38 Mar 28 '20 at 01:57
  • _"You can't use * for max height."_ -- Yes, I know that. But there's zero evidence in your question that you need to use the `MaxHeight` property at all. Your stated goal certainly does not require it. If you still think you need that property to be involved, then you need to state your goal differently (i.e. **more accurately**) in such a way that makes clear why that property is in fact needed. – Peter Duniho Mar 28 '20 at 02:13
  • I have a list box, and a button below it. The button always needs to be right below the list box, and there can't be any extra space in the list box. I need the list box to take up as much space on the screen as possible before it scrolls. Therefore, I want to set my max height to be a percentage of the screen (i.e. it takes up 90% max of the screen). Let me know if that makes sense to you and I'll put it in my answer. Otherwise, would you prefer if I deleted everything about my intent and just asked how to access the actualheight from my view model? – prelude38 Mar 30 '20 at 17:45
  • _"there can't be any extra space in the list box"_ -- that seems like an odd and user-unfriendly requirement. It means that the button is going to move around as the list box contents change. Moving buttons are bad. It's also not clear why you are approaching this from the perspective of a _percentage_ of the visible space. Given the newly-stated requirements though, I would use a converter. Your view model shouldn't be involved. Bind the inputs to the converter, do some math (subtract or multiply, depending on exact implementation) and return the `MaxHeight` value you want. – Peter Duniho Mar 30 '20 at 20:56
  • I can't use a converter, as the converter accesses ActualHeight before the page is loaded, so it always returns 0. If I rephrase the question to specifically ask for a way to access the actual window height from the view model and leave out all the UI, would you re-open the question? – prelude38 Mar 30 '20 at 21:18
  • I'll reopen the question anyway. I think you're really barking up the wrong tree, but if you insist on a solution not in keeping with the usual approaches, the ones I think are duplicates aren't going to help. – Peter Duniho Mar 30 '20 at 21:59
  • For what it's worth, I tried it with a converter, and it works fine. The only wrinkle I ran into was that the button does need an explicitly-given height, otherwise it tries to shrink to accommodate the list box. But that doesn't seem like that should be a problem. – Peter Duniho Mar 30 '20 at 22:07
  • And by abusing a second converter only a little bit, I found a way to not even have to explicitly set the height of the button. – Peter Duniho Mar 31 '20 at 00:06

0 Answers0