【发布时间】:2019-10-27 06:48:38
【问题描述】:
我正在尝试动态创建 SfSchedule。创建 Sf 计划很容易,但现在我需要添加网格和文本框来重新创建它,就像它在 .xaml 中一样
如何动态创建 DataTemplate 并动态添加网格和文本框?
我的 .xaml 中的代码工作正常,但我想动态创建它。到目前为止我所做的是使用 SfSchedule WeekSchedule = new SfSchedule();并为其属性分配值,但现在我需要动态创建 SfSchedule.AppointmentTemplate 和 DataTemplate,这是我尝试使用 DataTemplate Data = new DataTemplate();但它不允许我添加任何网格、矩形或文本框。
<syncfusion:SfSchedule ScheduleType="Month" Name="schedule" >
<syncfusion:SfSchedule.AppointmentTemplate>
<DataTemplate>
<Grid>
<Rectangle Fill="White" Stroke="Black"
StrokeThickness="3"></Rectangle>
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding AppointmentBackground}"
Width="10" ></Rectangle>
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="15"
Text="{Binding Subject}"
Foreground="{Binding AppointmentBackground}"
FontStyle="Normal"></TextBlock>
</StackPanel>
</Grid>
</DataTemplate>
</syncfusion:SfSchedule.AppointmentTemplate>
</syncfusion:SfSchedule>
C#
SfSchedule WeekSchedule = new SfSchedule();
WeekSchedule.HeaderDateFormat = "dddd dd";
DataTemplate DataTemp = new DataTemplate();
Grid firstGrid = new Grid();
DataTemp.Add(firstGrid); //This is what actually dont work, the datatemplate doesnt allow add
Rectange r1 = new Rectange();
r1.Fill = new SolidColorBrush(Colors.White);
r1.Stroke = new SolidColorBrush(Colors.Black);
r1.StrokeThickness = 3;
DataTemp.Add(r1);
WeekSchedule.AppointmentTemplate = DataTemp;
CalendarGrid.Children.Add(WeekSchedule);
预期的结果是能够将 Rectange 和 Grid 添加到 DataTemplate 中,然后将其添加到 apppointmenttemplate 中,然后添加到计划中。
这基本上是一个用于测试的虚拟代码,我想知道是否可以这样做?
谢谢
【问题讨论】:
-
为什么不在 XAML 中执行此操作?在 C# 中创建数据模板很麻烦。
-
编辑:当访问带有日历的窗口时,由于某些旧版本的 Windows 出现黑屏问题。
-
您认为通过在 C# 中创建 DataTemplate,您将能够解决这个问题?
-
为页面设置 NavigationCacheMode 为启用,解决了黑屏问题,但性能下降很大。我们的想法是清除最大内存但保留缓存,以便在每次访问屏幕时通过代码动态创建日历来不损失性能
标签: c# wpf xaml windows-runtime syncfusion