【问题标题】:Binding drives me Crazy绑定让我发疯
【发布时间】:2012-03-14 06:05:33
【问题描述】:

编辑:

XAML:

<TextBlock Text="{Binding Path=CurrentShows}"  Grid.Row="1"/>

产生以下输出:

 SilverlightHelloWorld.Deserialize.schedule

XAML:

<TextBlock Text="{Binding Path=CurrentShows.Today}"  Grid.Row="1"/>

也没有

<TextBlock Text="{Binding Path=CurrentShows.Today.Date}"  Grid.Row="1"/>

在调试模式下产生任何错误或输出。

有什么建议吗?


旧语句:

我在这里有一个非常复杂的示例, 最好的是,我从我的代码开始。

这是我的代码隐藏:

Mainpage.xaml.cs

schedule currentshows;
    public schedule CurrentShows
    {
        protected set
        {
            if (currentshows != value)
            {
                currentshows = value;
                OnPropertyChanged("CurrentShows");
            }
        }
        get
        {
            return currentshows;
        }
    }

schedule.cs

[XmlRoot]
    public class schedule : INotifyPropertyChanged 
    {
        public event PropertyChangedEventHandler PropertyChanged;


        DayCollection today;
        [XmlElement("DAY")]
        public DayCollection Today
        {
            set
            {
                if (today != value)
                {
                    today = value;
                    OnPropertyChanged("Today");

                }
            }
            get
            {
                return today;
            }
        }

        private void OnPropertyChanged(string p)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(p));
        }

    }

DayCollection.cs

    public class DayCollection : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        ObservableCollection<TimeCollection> timecol;
        [XmlElement("time")]
        public ObservableCollection<TimeCollection> TimeCol
        {
            set
            {
                if (timecol != value)
                {
                    timecol = value;
                    OnPropertyChanged("TimeCol");

                }
            }
            get
            {
                return timecol;
            }
        }

        string date;
        [XmlAttribute(AttributeName = "attr")]
        public string Date
        {
            set 
            {
                if (date != value)
                {
                    date = value;
                    OnPropertyChanged("Date");

                }
            }
            get
            {
                return date;
            }
        }

        private void OnPropertyChanged(string p)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(p));
        }

    }

我试图让我的 Xaml 中的字符串属性“日期”显示出来。 但我只是没有得到任何解决方案。

我非常感谢这里的任何帮助,在此先感谢!

【问题讨论】:

  • 您需要做的第一件事是打开数据绑定的调试消息:i.stack.imgur.com/MF8i5.png 接下来,重新运行并检查输出窗口,看看有什么错误。
  • 不是标准的吗?好吧,我再次检查,甚至在错误时将它们打开,并且它编译得很好。没有错误,没有警告,什么都没有。
  • 不,不是在编译时,而是在运行时。
  • 即使运行程序,也没有任何显示。只是为了提供一些 xaml 代码...&lt;TextBlock Text="{Binding Path=CurrentShows.Today.Date}" Grid.Row="1"/&gt; &lt;TextBlock Text="{Binding Path=Shows[0].ShowName}" /&gt; Lasttextblock 已正确填充,上面的那些只是没有显示任何内容。
  • 我可以保证,如果您的绑定有问题,如果您按照该图像所示设置了选项,则会在调试时报告该问题。因此,要么您的问题在其他地方(很可能),要么您的 VS 安装不好(不太可能)。首先要做的是关闭并重新启动 VS,然后重试。

标签: c# silverlight xaml data-binding inotifypropertychanged


【解决方案1】:

我已将您的代码复制到一个项目中,如果您使用此行来设置 TextBlock 的绑定,它就可以工作

<TextBlock Text="{Binding Today.Date}"  Grid.Row="1"/>

但我无法从您的代码中看出您是如何设置外部数据绑定的。这是我在 MainPage 构造函数中包含的内容

currentshows = new schedule();
currentshows.Today = new DayCollection();
currentshows.Today.Date = "hallo";
LayoutRoot.DataContext = currentshows;

其中 LayoutRoot 是 TextBlock 的父级,即

<Grid x:Name="LayoutRoot">
 <TextBlock Text="{Binding Today.Date}"/> 
</Grid>

【讨论】:

    猜你喜欢
    • 2011-05-30
    • 2012-11-02
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 2021-04-20
    相关资源
    最近更新 更多