Hi there,
I would be appreciate your help with this.
Attached a screenshot that shows what I am looking for:
Thanks,
Bahram Afsharipoor
7 Answers, 1 is accepted
Hi Bahram,
Thank you for the provided image.
You can get ahold of all dates in the viewport by getting all MajorTickContainer instances with the ChildrenOfType extension method.
var ticks = this.GanttView.ChildrenOfType<MajorTickContainer>().ToList();
var visibleDates = new List<DateTime>();
for (int i = 0; i < ticks.Count(); i++)
{
var tick = ticks[i] as MajorTickContainer;
var vm = tick.DataContext as TickProxy;
var date = vm.DateTime;
if (tick.Visibility == Visibility.Visible)
{
visibleDates.Add(date);
}
}
var first = visibleDates.OrderBy(x => x.Date).First();
var last = visibleDates.OrderBy(x => x.Date).Last();
MessageBox.Show(string.Join(", ", visibleDates.OrderBy(x => x.Date).Select(x => x.Day)));
Please give this a try and let me know if such an approach works for you.
Regards,
Dilyan Traykov
Progress Telerik
Hi Dilyan,
I appreciate you giving me this solution, it is exactly what I needed!
Regards,
Bahram Afsharipoor
Hi Dilyan,
I have found an issue with above code. When I click on repeat buttons (transparent areas) of gantt's horizontal scroll, I get a completely different set of visible dates. I don't know what is happening, can you please help me with that?
Attached is a picture that shows the clicked areas of horizontal scroll.
Thanks,
Bahram
Hi Bahram,
Thank you for the provided image.
I tried replicating the issue you described at my end by clicking the areas you referenced, however, the approach I proposed seems to return the correct dates each time.
I've attached the project I set up as well as a short recording of the result I observe at my end.
Could you please have a look and let me know if I'm missing something of importance?
Regards,
Dilyan Traykov
Progress Telerik
Hi Dilyan,
Your code works perfectly fine if I run it via the click event of that button you added to your attached project. However, I am trying to get visible dates of my gantt view through its scroll event. Basically as user scrolls horizontally, I want to be able to get current visible dates. If you try to run your code via the scroll event of gantt view, you will be able to reproduce the issue.
I am guessing that the datacontext of MajorTickContainer class gets updated after the scroll event of gantt view finished executing, am I right? If so, what do you suggest me to do?
Thanks,
Bahram Afsharipoor
Hi Bahram,
I assume that by "the scroll event of gantt view" you reference the attached ScrollBar.Scroll event. Please correct me if I'm wrong in this assumption.
If this is indeed the event you're handling, you need to invoke the logic for getting the visible dates through the Dispatcher's BeginInvoke method and specify DispatcherPriority.Render as the priority.
private void GanttView_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
{
Dispatcher.BeginInvoke(new Action(() =>
{
var ticks = this.GanttView.ChildrenOfType<MajorTickContainer>().ToList();
var visibleDates = new List<DateTime>();
for (int i = 0; i < ticks.Count(); i++)
{
var tick = ticks[i] as MajorTickContainer;
var vm = tick.DataContext as TickProxy;
var date = vm.DateTime;
if (tick.Visibility == Visibility.Visible)
{
visibleDates.Add(date);
}
}
var first = visibleDates.OrderBy(x => x.Date).First();
var last = visibleDates.OrderBy(x => x.Date).Last();
MessageBox.Show(string.Join(", ", visibleDates.OrderBy(x => x.Date).Select(x => x.Day)));
}), System.Windows.Threading.DispatcherPriority.Render);
}
Regards,
Dilyan Traykov
Progress Telerik
Hi Dilyan,
Yes this is exactly what I needed, thank you for your help!
Thanks,
Bahram Afsharipoor