简体   繁体   中英

C# WPF How to create TextBlock programmatically with parameter

I'm trying create GanttChart using Class GantRowPanel from this article: GanttChart

In my MainWindow.xaml I have:

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

How to add TextBlock programmatically, from CodeBehind to Gantt ?

I was trying this:

在此处输入图片说明

But it doesn't work.

I have found the problem, the DateTime range is not right. Without setting the date, the default date will be 0001-1-1 , which not contains the date range in the code-behind. So you should keep the same DateTime style in the XAML and code-behind.

You should add date to the MinDate and the MaxDate in the XAML:

<UI:GanttRowPanel x:Name="Gantt" MinDate="2017-10-15 09:00" MaxDate="2017-10-15 17:00">
</UI:GanttRowPanel>

Alternatively, remove the date in the code-behind:

ui.GanttRowPanel.SetStartDate(txBlock, DateTime.Parse("09:00"));
ui.GanttRowPanel.SetEndDate(txBlock, DateTime.Parse("15:00")); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM