尝试将网格的高度/宽度绑定到ViewModel

时间:2012-04-21 21:55:34

标签: c# wpf xaml data-binding

所以我在视图框中有一个网格。现在,网格与视图框很好地缩放。但是,我需要知道ViewModel中网格的高度和宽度。然而,它似乎没有将高度设置为任何东西?

<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>

在我的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");
        }
    }

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

你需要在OneWayToSource / ActualWidth上绑定Height,因为这些属性是只读的,到目前为止我已经看到了任何好的解决方案,因为WPF完全拒绝绑定只读依赖属性。