[英]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.