C# 为什么不能在Silverlight中绑定Grid.RowDefinition高度?
当我运行以下Silverlight应用程序时,它会给我错误: 语法分析器错误属性值[行: 12位置:35] 我在WPF中尝试了相同的代码,它运行fine,即中间网格行根据绑定值正确调整大小 为了避免在Silverlight中出现此错误,我必须对此代码进行哪些更改? XAML:C# 为什么不能在Silverlight中绑定Grid.RowDefinition高度?,c#,silverlight,grid,data-binding,C#,Silverlight,Grid,Data Binding,当我运行以下Silverlight应用程序时,它会给我错误: 语法分析器错误属性值[行: 12位置:35] 我在WPF中尝试了相同的代码,它运行fine,即中间网格行根据绑定值正确调整大小 为了避免在Silverlight中出现此错误,我必须对此代码进行哪些更改? XAML: <UserControl x:Class="TestRowHeight222.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pre
<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
}
}
这是我能得到的最接近的,我不知道它是否适合你的情况
<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属性更改为双精度。即使我将ContentHeight ViewModel属性从int更改为GridLength,然后将其定义为ContentHeight=new GridLength(100,GridUnitType.Pixel);它给了我同样的错误,我的道歉,不像我第一次看到的那样直截了当。我自己也试了一下,没能成功。这篇文章的最后一篇文章似乎解释了原因:我怀疑这是不可能的,我想知道是否有一些创造性的解决办法。请看上面编辑的答案,这是我的想法。这样做有一两个明显的缺点,所以我不知道在这种情况下是否合适。