捆绑让我发疯
本文关键字:发疯 | 更新日期: 2023-09-27 18:25:40
编辑:
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"/>
在调试模式下产生任何错误或输出。
有什么建议吗?
旧声明:
我这里有一个安静而复杂的例子,最好的是,我从我的代码开始。
这是我的代码背后:
主页.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"以显示。但我只是没有任何解决方案。
我非常感谢这里的任何帮助,提前谢谢!
我已经将您的代码复制到一个项目中,如果您使用这一行来设置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>