0

So I have a grid inside a viewbox. Now the grid scales with the viewbox fine. However, I need to know the height and width of the grid in my ViewModel. However it doesn't seem to ever set the Height to anything?

<Viewbox VerticalAlignment="Top" Margin="5,20,5,0">
            <Border BorderThickness="6" BorderBrush="Black" CornerRadius="2">

                <Grid MinHeight="300" MinWidth="400" Height="{Binding Height, Mode=TwoWay}" Width="{Binding Width, Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="0" >
                    <ItemsControl ItemsSource="{Binding Viewers}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <Canvas />
                            </ItemsPanelTemplate>
                             </ItemsControl.ItemsPanel>
                             <ItemsControl.ItemContainerStyle>
                            <Style>
                                <Setter Property="Canvas.Left" Value="{Binding X}" />
                                <Setter Property="Canvas.Top" Value="{Binding Y}"/>
                            </Style>
                        </ItemsControl.ItemContainerStyle>
                    </ItemsControl>
                </Grid>
            </Border>
        </Viewbox>

And in my ViewModel:

    private int _height;
    public int Height
    {
        get
        {
            return _height;
        }

        set
        {
            _height = value;
            OnPropertyChanged("Height");
        }
    }

    private int _width;
    public int Width
    {
        get
        {
            return _width;
        }

        set
        {
            _width = value;
            OnPropertyChanged("Width");
        }
    }

Any ideas?

H.B.
  • 166,899
  • 29
  • 327
  • 400
benjgorman
  • 702
  • 10
  • 31
  • And you have the right `DataContext`? – H.B. Apr 21 '12 at 21:58
  • There isn't a problem with any of the other DataBindings for the same View/ViewModel so I assume so. – benjgorman Apr 21 '12 at 21:59
  • I hope you don't keep the aspect ratio when changing values then, because that would only be visible in the corner radius of the Border. – H.B. Apr 21 '12 at 22:01
  • Not sure what you mean, the real problem here is that Height and Width are always 0 and the OnPropertyChanged events are never fired. – benjgorman Apr 21 '12 at 22:06
  • Width and Height is not the same as ActualWidth and ActualHeight... – erikH Apr 21 '12 at 22:09
  • Ah that was an obvious one. Never realized that. Will look up how to do that then. – benjgorman Apr 21 '12 at 22:18
  • A larger problem You will find... A bug that you can't bind to readonly dependency properties. [One workaround](http://stackoverflow.com/questions/1083224/pushing-read-only-gui-properties-back-into-viewmodel). – erikH Apr 22 '12 at 06:27

1 Answers1

0

You would need a OneWayToSource binding on ActualWidth/Height as those properties are readonly, so far i have seen any good solutions to that as WPF reject bindings outright if set on a readonly dependency property.

H.B.
  • 166,899
  • 29
  • 327
  • 400