【问题标题】:Why can I not bind Grid.RowDefinition Height in Silverlight?为什么我不能在 Silverlight 中绑定 Grid.RowDefinition Height?
【发布时间】:2011-01-25 10:49:07
【问题描述】:

当我运行以下 Silverlight 应用程序时,它给了我错误

AG_E_PARSER_BAD_PROPERTY_VALUE [行: 12位:35]

我在 WPF 中尝试了相同的代码,它运行 fine,即中间的网格行根据绑定值正确调整大小。

我必须在此代码中进行哪些更改才能避免 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
    }
}

【问题讨论】:

    标签: c# silverlight grid data-binding


    【解决方案1】:

    这个尽量接近了,不知道是否适合你的情况。

    <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 ViewModelProperty 从 int 更改为 GridLength 然后将其定义为 ContentHeight = new GridLength(100, GridUnitType.Pixel);它给了我同样的错误
    • 我很抱歉,并不像最初在我看来那么简单。我自己也尝试过,但无法正常工作。该线程中的最后一篇文章似乎解释了原因:forums.silverlight.net/forums/t/95363.aspx
    • 我怀疑这是不可能的,但我想知道是否有一些创造性的解决方法
    • 查看上面的编辑答案,这是我的尝试。这样做有一个或两个明显的缺点,所以我不知道它是否适合这种情况。
    猜你喜欢
    • 2011-11-07
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    相关资源
    最近更新 更多