咨询.NET MAUI中Syncfusion/DevExpress日程控件多属性复用方案
解决方案:.NET MAUI 日程控件多属性绑定问题
Syncfusion SfScheduler 解决办法
方式1:用自定义映射扩展字段
Syncfusion的Scheduler支持通过CustomMapping添加额外的属性映射,这样就能把业务对象里的StatusColor这类字段绑定到预约项上,步骤如下:
- 确保你的业务模型里有
UserColor、StatusColor这些字段 - 修改映射配置,添加自定义映射:
<scheduler:SfScheduler.AppointmentMapping> <scheduler:SchedulerAppointmentMapping StartTime="EventStartDateTime" EndTime="EventEndDateTime" Subject="PatientName" Background="UserColor"> <!-- 新增自定义映射,把业务对象的StatusColor映射到预约项的StatusColor属性 --> <scheduler:SchedulerAppointmentMapping.CustomMapping> <scheduler:SchedulerCustomMapping Name="StatusColor" MappingName="StatusColor" /> </scheduler:SchedulerAppointmentMapping.CustomMapping> </scheduler:SchedulerAppointmentMapping> </scheduler:SfScheduler.AppointmentMapping>
- 调整模板,直接绑定自定义的
StatusColor:
<ContentPage.Resources> <DataTemplate x:Key="dayAppointmentTemplate"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <!-- 绑定自定义映射的StatusColor,替换硬编码颜色 --> <BoxView Background="{Binding StatusColor}" WidthRequest="4" VerticalOptions="FillAndExpand" Grid.Column="0"/> <StackLayout Background="{Binding Background}" Grid.Column="1"> <Label Text="{Binding Subject}" TextColor="#3B3E45" LineBreakMode="TailTruncation" FontSize="14" FontFamily="Noto Sans" Margin="8,2,0,0" HorizontalOptions="Start" VerticalOptions="Start"/> <Label Text="{Binding Notes}" TextColor="#666E7D" LineBreakMode="TailTruncation" FontSize="10" FontFamily="Noto Sans" Margin="8,0,5,5" HorizontalOptions="Start" VerticalOptions="Start"/> <!-- 其他自定义字段比如图标,也可以用同样的方式添加CustomMapping后绑定 --> <Image Source="{Binding CustomIcon}" Margin="8,0" WidthRequest="16" HeightRequest="16"/> </StackLayout> </Grid> </DataTemplate> </ContentPage.Resources>
方式2:直接绑定业务对象原始属性
如果觉得自定义映射麻烦,还有更直接的方式:因为预约项的BindingContext就是你的原始业务对象(只要你是把业务集合直接绑定到SfScheduler.AppointmentsSource),所以模板里可以跳过映射,直接绑定业务对象的字段:
修改模板里的绑定:
<!-- 直接绑定业务对象的StatusColor --> <BoxView Background="{Binding StatusColor}" WidthRequest="4" VerticalOptions="FillAndExpand" Grid.Column="0"/> <!-- 直接绑定业务对象的UserColor,不用走Background映射 --> <StackLayout Background="{Binding UserColor}" Grid.Column="1"> <!-- 其他控件绑定保持不变 --> </StackLayout>
这种方式不用改映射配置,适合需要绑定多个自定义字段的场景。
备选:DevExpress MAUI Scheduler
DevExpress的日程控件对自定义属性的支持更灵活,不需要配置任何映射,直接在模板里绑定业务对象的任意字段就行:
- 把你的业务集合绑定到
DxScheduler.AppointmentsSource - 自定义预约模板:
<DataTemplate x:Key="CustomAppointmentTemplate"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <BoxView Background="{Binding StatusColor}" WidthRequest="4" VerticalOptions="FillAndExpand"/> <StackLayout Grid.Column="1" Background="{Binding UserColor}"> <Label Text="{Binding PatientName}" FontSize="14"/> <Label Text="{Binding AppointmentStatus}" FontSize="10"/> <!-- 其他自定义属性直接绑定即可,不用额外配置 --> </StackLayout> </Grid> </DataTemplate>
然后通过DxScheduler.DayView.AppointmentTemplate指定这个模板就可以了,整个流程更简洁。
内容的提问来源于stack exchange,提问作者Vladyslav Malin
相关产品推荐
相关产品推荐

