如何从XAML内部的代码绑定数据
我有这门课:如何从XAML内部的代码绑定数据,xaml,xamarin.forms,data-binding,Xaml,Xamarin.forms,Data Binding,我有这门课: public partial class Calendar : ContentPage, INotifyPropertyChanged { public ICommand EventSelectedCommand => new Command(async (item) => await ExecuteEventSelectedCommand(item)); public ICommand DayTappedCommand => new Comman
public partial class Calendar : ContentPage, INotifyPropertyChanged
{
public ICommand EventSelectedCommand => new Command(async (item) => await ExecuteEventSelectedCommand(item));
public ICommand DayTappedCommand => new Command<DateTime>(async (date) => await DayTapped(date));
public EventCollection Events { get; set; }
private void SetCalendar()
{
Events = new EventCollection
{
[DateTime.Now] = new List<EventModel>
{
new EventModel { Name = "Herrenabend, 10:00 - 12:00 – 5000 Scanns", ID = 100},
};
}
公共部分类日历:ContentPage,INotifyPropertyChanged
{
公共ICommand EventSelectedCommand=>新命令(异步(项)=>等待执行EventSelectedCommand(项));
public ICommand DayTappedCommand=>new命令(异步(日期)=>await DayTapped(日期));
公共事件集合事件{get;set;}
私有void SetCalendar()
{
Events=新的EventCollection
{
[DateTime.Now]=新列表
{
新事件模型{Name=“Herrenabend,10:00-12:00–5000扫描”,ID=100},
};
}
我想把数据绑定到我的日历上。
我试过这个:
<Grid Grid.Row="1" Grid.Column="1">
<controls:Calendar
x:Name="calender"
DayTappedCommand="{Binding DayTappedCommand}"
Events="{Binding Q2go.MainMenu_Partners.Calendar.Events}">
<controls:Calendar.EventTemplate>
<DataTemplate>
<StackLayout>
<Label
Text="{Binding Name}"
FontAttributes="Bold"
FontSize="Medium" >
<Label.GestureRecognizers>
<TapGestureRecognizer
Tapped="ClickOnLabel"
/>
</Label.GestureRecognizers>
</Label>
<Label
Grid.Row="1"
Text="{Binding ID}"
FontSize="8" >
<Label.GestureRecognizers>
<TapGestureRecognizer
Tapped="ClickOnLabel"
/>
</Label.GestureRecognizers>
</Label>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.EventSelectedCommand, Source={x:Reference test}}"
CommandParameter="{Binding .}" />
</StackLayout.GestureRecognizers>
</StackLayout>
</DataTemplate>
</controls:Calendar.EventTemplate>
</controls:Calendar>
</Grid>
但是这些数据并没有被记录在日历上,也没有被记录在火灾事件中。
这里丢失的链接在哪里
我想:Events=“{Binding Q2go.main menu\u Partners.Calendar.Events}>
我做的每件事都是正确的。但是什么都没有显示。为什么
我知道,如果我只是在代码(calendar.events=events)内部将数据提供给日历,它就可以正常工作(仍然没有事件处理程序),但我希望它从代码绑定数据。因此,没有这一行(calendar.events=events)
我在每个示例中都读到这应该有效,但对我来说无效。那么问题在哪里呢?如果页面的
BindingContext
设置为this
,那么您应该只使用Events=“{Binding Events}”
作为绑定表达式如果页面的BindingContext
设置为this
,则只需使用Events=“{Binding Events}”
作为绑定表达式精彩绝伦。非常感谢!@user13397492嗨,你已经解决了这个问题吗?如果是的话,记得在你有时间的时候在回答中分享解决方案。是的,在@Jason的帮助下。我只是错过了这一行代码。他应该回答,这样我就可以接受:)@Jason请把它作为答案,以便op结束问题。