微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

当我运行以下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
    }
}

解决方法:

这是我能得到的,我不知道它是否适合你的情况.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐