2012-04-21 52 views
0

因此,我在视图框内有一个网格。现在,网格与视图框精确匹配。但是,我需要知道ViewModel中网格的高度和宽度。然而,它似乎并没有将高度设置为任何东西?尝试将网格的高度/宽度绑定到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"); 
     } 
    } 

任何想法?

+0

你有正确的'DataContext'? – 2012-04-21 21:58:27

+0

对于相同View/ViewModel的任何其他DataBindings都没有问题,所以我假设如此。 – benjgorman 2012-04-21 21:59:26

+0

我希望在改变数值时不要保持长宽比,因为那只会在边框的角半径中可见。 – 2012-04-21 22:01:05

回答

0

您将需要一个OneWayToSourceActualWidth/Height因为这些属性是只读的结合,到目前为止,我已经看到了这什么好的解决方案,WPF拒绝绑定彻底如果只读依赖属性设置。

相关问题