簡體   English   中英

C# WPF 如何使用參數以編程方式創建 TextBlock

[英]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 中的MinDateMaxDate

<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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM