为什么我不能在Silverlight中绑定Grid.RowDefinition的高度?

4
当我运行以下的Silverlight应用程序时,它给了我一个错误: AG_E_PARSER_BAD_PROPERTY_VALUE [Line:12 Position: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
    }
}
1个回答

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类型。


即使我将ContentHeight ViewModelProperty从int更改为GridLength,然后将其定义为ContentHeight = new GridLength(100,GridUnitType.Pixel); 它仍然会给我相同的错误。 - Edward Tanguay
非常抱歉,这对我来说并不像最初看起来那么简单。我也尝试了一下,但无法使其正常工作。此线程中的最后一篇帖子似乎解释了原因:http://forums.silverlight.net/forums/t/95363.aspx - Dan Wray
我怀疑这是不可能的,但我想知道是否有一些创造性的解决方法。 - Edward Tanguay
请参考上面编辑过的答案,那是我尽力而为的翻译。这种方式显然有一两个明显的缺点,所以我不知道它是否适用于这种情况。 - Dan Wray

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接