[英]C# WPF How to create TextBlock programmatically with parameter
我正在尝试使用这篇文章中的GantRowPanel类创建 GanttChart: GanttChart
在我的 MainWindow.xaml 中,我有:
<UI:GanttRowPanel x:Name="Gantt" MinDate="09:00" MaxDate="17:00">
<TextBlock Text="Work" UI:GanttRowPanel.StartDate="09:00" UI:GanttRowPanel.EndDate="12:00"/>
<TextBlock Text="Lunch" UI:GanttRowPanel.StartDate="12:00" UI:GanttRowPanel.EndDate="13:00"/>
<TextBlock Text="Work" UI:GanttRowPanel.StartDate="13:00" UI:GanttRowPanel.EndDate="17:00"/>
</UI:GanttRowPanel>
如何以编程方式添加 TextBlock,从 CodeBehind 到 Gantt ?
我正在尝试这个:
但它不起作用。
我发现问题了, DateTime
范围不对。 如果不设置日期,默认日期将为0001-1-1
,其中不包含代码隐藏中的日期范围。 因此,您应该在 XAML 和代码隐藏中保持相同的DateTime
样式。
您应该将date
添加到 XAML 中的MinDate
和MaxDate
:
<UI:GanttRowPanel x:Name="Gantt" MinDate="2017-10-15 09:00" MaxDate="2017-10-15 17:00">
</UI:GanttRowPanel>
或者,删除代码隐藏中的date
:
ui.GanttRowPanel.SetStartDate(txBlock, DateTime.Parse("09:00"));
ui.GanttRowPanel.SetEndDate(txBlock, DateTime.Parse("15:00"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.