绑定让我疯狂

时间:2012-02-23 19:21:06

标签: c# silverlight xaml data-binding inotifypropertychanged

编辑:

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"/>

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

Anysuggestions?


OldStatement:

我这里有一个很安静很复杂的例子, 最好的是,我从我的代码开始。

这是我的Codebehind:

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中获取字符串属性“Date”以显示。 但我只是没有得到任何解决方案。

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

1 个答案:

答案 0 :(得分:0)

我已将您的代码复制到项目中,如果您使用此行代替设置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>