2010-03-09 64 views
4

當我運行以下Silverlight的應用,它給了我錯誤爲什麼我不能在Silverlight中綁定Grid.RowDefinition Height?

AG_E_PARSER_BAD_PROPERTY_VALUE [行: 12職位:35]

我試過在同一個代碼WPF它運行罰款,即中間網格行正確調整大小的基礎上的界限值。

爲了避免Silverlight中的這個錯誤,我必須在這段代碼中改變什麼?

XAML:

<UserControl x:Class="TestRowHeight222.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"> 
    <Grid x:Name="LayoutRoot"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="30"/> 
      <RowDefinition Height="{Binding ContentHeight}"/> 
      <RowDefinition Height="30"/> 
     </Grid.RowDefinitions> 

     <StackPanel Grid.Row="0" Background="Tan"> 
      <TextBlock Text="row0" /> 
     </StackPanel> 

     <StackPanel Grid.Row="1" Background="Beige" Orientation="Horizontal"> 
      <TextBlock Text="The height should be: "/> 
      <TextBlock Text="{Binding ContentHeight}"/> 
     </StackPanel> 

     <StackPanel Grid.Row="2" Background="Tan"> 
      <TextBlock Text="row2"/> 
     </StackPanel> 
    </Grid> 
</UserControl> 

代碼背後:

using System.Windows.Controls; 
using System.ComponentModel; 

namespace TestRowHeight222 
{ 
    public partial class MainPage : UserControl, INotifyPropertyChanged 
    { 
     #region ViewModelProperty: ContentHeight 
     private int _contentHeight; 
     public int ContentHeight 
     { 
      get 
      { 
       return _contentHeight; 
      } 

      set 
      { 
       _contentHeight = value; 
       OnPropertyChanged("ContentHeight"); 
      } 
     } 
     #endregion 

     public MainPage() 
     { 
      InitializeComponent(); 
      DataContext = this; 
      ContentHeight = 50; 
     } 

     #region INotifiedProperty Block 
     public event PropertyChangedEventHandler PropertyChanged; 

     protected void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 

      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
     #endregion 
    } 
} 

回答

4

這是接近我可以得到的,我不知道這是否是適合你的情況。

<Grid x:Name="LayoutRoot"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="30"/> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="30"/> 
    </Grid.RowDefinitions> 

    <StackPanel Grid.Row="0" Background="Tan"> 
     <TextBlock Text="row0" /> 
    </StackPanel> 

    <Grid Grid.Row="1" Height="{Binding ContentHeight}"> 
     <StackPanel Background="Beige" Orientation="Horizontal"> 
      <TextBlock Text="The height should be: "/> 
      <TextBlock Text="{Binding ContentHeight}"/> 
     </StackPanel> 
    </Grid> 

    <StackPanel Grid.Row="2" Background="Tan"> 
     <TextBlock Text="row2"/> 
    </StackPanel> 
</Grid> 

還將您的ContentHeight屬性更改爲double。

+0

即使我將我的ContentHeight ViewModelProperty從int更改爲GridLength,然後將其定義爲ContentHeight = new GridLength(100,GridUnitType.Pixel);它給了我同樣的錯誤 – 2010-03-09 14:08:23

+0

我的道歉,並不像我第一次看到的那樣直截了當。我也有自己的一個去,並不能得到它的工作。 此主題的最後發佈似乎解釋了原因:http://forums.silverlight.net/forums/t/95363.aspx – 2010-03-09 14:35:13

+0

我懷疑這是不可能的,不知是否有一些創造性的解決方法,雖然 – 2010-03-09 14:37:19

相關問題