2010-03-09 1 views
4

Quand je lance l'application Silverlight suivante, il me donne l'erreur :Pourquoi ne puis-je pas lier la hauteur Grid.RowDefinition dans Silverlight?

AG_E_PARSER_BAD_PROPERTY_VALUE [Ligne: 12 Position: 35]

J'ai essayé le même code dans WPF et il exécute fine, c'est-à-dire la ligne de la grille du milieu se redimensionne correctement en fonction de la valeur liée.

Que dois-je modifier dans ce code pour éviter cette erreur dans 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> 

code Derrière:

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

Répondre

4

C'est aussi proche que je peux obtenir, je ne sais pas si elle est adaptée à votre situation.

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

Modifiez également votre propriété ContentHeight en double.

+0

même si je change ContentHeight ViewModelProperty de int à GridLength et que je le définis comme ContentHeight = new GridLength (100, GridUnitType.Pixel); ça me donne la même erreur –

+0

Mes excuses, pas aussi simple que cela me semblait. J'y suis allé aussi et je ne pouvais pas le faire fonctionner. Dernier message dans ce fil semble expliquer pourquoi: http://forums.silverlight.net/forums/t/95363.aspx –

+0

Je me doutais que ce n'est tout simplement pas possible, je me demande s'il existe une solution de rechange créative si –

Questions connexes