我正在尝试动态创建一个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);预期的结果是能够将矩形和网格添加到DataTemplate中,然后将其添加到apppointmenttemplate中,然后添加到计划中。
这基本上是一个用于测试的虚拟代码,我想知道是否可以这样做?
谢谢
发布于 2019-06-17 18:21:47
使用以下代码片段来解决您的问题。在下面的代码中,我们使用FrameworkElementFactory而不是FramWorkElement。
DataTemplate appointmentTemplate = new DataTemplate();
appointmentTemplate.DataType = typeof(ScheduleDaysAppointmentViewControl);
FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid));
grid.SetValue(Grid.BackgroundProperty, new SolidColorBrush(Colors.Red));
grid.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
FrameworkElementFactory rect = new FrameworkElementFactory(typeof(Rectangle));
rect.SetValue(Rectangle.FillProperty, new SolidColorBrush(Colors.White));
rect.SetValue(Rectangle.StrokeProperty, new SolidColorBrush(Colors.Black));
rect.SetValue(Rectangle.StrokeThicknessProperty, 3d);
grid.AppendChild(rect);
appointmentTemplate.VisualTree = grid;
schedule.AppointmentTemplate = appointmentTemplate;并在下面的链接中找到相同的样本。
示例:http://www.syncfusion.com/downloads/support/directtrac/general/ze/SfSchedule_WPF983671020
致敬,Magesh S
https://stackoverflow.com/questions/56567551
复制相似问题