简体   繁体   English

C# WPF 如何使用参数以编程方式创建 TextBlock

[英]C# WPF How to create TextBlock programmatically with parameter

I'm trying create GanttChart using Class GantRowPanel from this article: GanttChart我正在尝试使用这篇文章中的GantRowPanel类创建 GanttChart: GanttChart

In my MainWindow.xaml I have:在我的 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>

How to add TextBlock programmatically, from CodeBehind to Gantt ?如何以编程方式添加 TextBlock,从 CodeBehind 到 Gantt ?

I was trying this:我正在尝试这个:

在此处输入图片说明

But it doesn't work.但它不起作用。

I have found the problem, the DateTime range is not right.我发现问题了, DateTime范围不对。 Without setting the date, the default date will be 0001-1-1 , which not contains the date range in the code-behind.如果不设置日期,默认日期将为0001-1-1 ,其中不包含代码隐藏中的日期范围。 So you should keep the same DateTime style in the XAML and code-behind.因此,您应该在 XAML 和代码隐藏中保持相同的DateTime样式。

You should add date to the MinDate and the MaxDate in the XAML:您应该将date添加到 XAML 中的MinDateMaxDate

<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:或者,删除代码隐藏中的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