0

I have a XAML element (grid) which has it's horizontal and vertical alignment set to "Stretch".

I want to get the width and the height of the grid when it's on the screen and reflect that to my property in my viewmodel (not the property telling the grid how big it should be but the other way around).

I've tried multiple variations of this but none work:

        <Grid Margin="100" Name="mainHMapGrid" Background="Transparent" 
                                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                                  Width="{Binding HMapGridWidth, Mode=OneWay,
                                    UpdateSourceTrigger=PropertyChanged}"
                                  Height="{Binding HMapGridHeight, Mode=OneWay,
                                    UpdateSourceTrigger=PropertyChanged}

        <Grid Margin="100" Name="mainHMapGrid" Background="Transparent" 
                                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                                  Width="{Binding HMapGridWidth, Mode=OneWayToSource,
                                    UpdateSourceTrigger=PropertyChanged}"
                                  Height="{Binding HMapGridHeight, Mode=OneWayToSource,
                                    UpdateSourceTrigger=PropertyChanged}

        <Grid Margin="100" Name="mainHMapGrid" Background="Transparent" 
                                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                                  Width="{Binding HMapGridWidth, Mode=TwoWay,
                                    UpdateSourceTrigger=PropertyChanged}"
                                  Height="{Binding HMapGridHeight, Mode=TwoWay,
                                    UpdateSourceTrigger=PropertyChanged}

What am I missing?...

uaswpff
  • 75
  • 6
  • 1
    See here: https://stackoverflow.com/a/1083733/1136211. It is ActualWidth and ActualHeight which actually change. However, you can't bind them directly. – Clemens Jan 29 '20 at 13:35
  • sigh... why don't Microsoft make things a bit easier for us. Why don't they let you bind directly to these?? – uaswpff Jan 29 '20 at 13:37
  • Why don't you just subscribe to the Grid's SizeChanged event? That's totally easy. – Clemens Jan 29 '20 at 13:40
  • Hmm, OK I can make an ICommand for that... but can you actually *change* the Grid's width and height? – uaswpff Jan 29 '20 at 13:41
  • Seriously though, is there a specific logical reason why they don't let you bind to readonly DPs? – uaswpff Jan 29 '20 at 13:42

0 Answers0