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");
}
}
任何想法?
你有正確的'DataContext'? – 2012-04-21 21:58:27
對於相同View/ViewModel的任何其他DataBindings都沒有問題,所以我假設如此。 – benjgorman 2012-04-21 21:59:26
我希望在改變數值時不要保持長寬比,因爲那隻會在邊框的角半徑中可見。 – 2012-04-21 22:01:05