9

enter image description here

foreach (DataRow dr in dt.Rows)
{
    Rectangle rectangle_timeline = new Rectangle();
    rectangle_timeline.Height = 19;
    rectangle_timeline.Cursor = Cursors.Hand;

    rectangle_timeline.Effect = new DropShadowEffect
    {
        Color = new Color { A = 255, R = 0, G = 0, B = 0 },
        Direction = 315,
        ShadowDepth = 5,
        Opacity = 1
    };

    Grid_Timeline.Children.Add(rectangle_timeline);
}

I dynamically add a Rectangle with above simple code as shown image.

However, sometimes, randomly, there're rectangles without DropShadowEffect like yellow rectangles and 1 blue rectangle at the lowest.

As you see the code, if a rectangle is added, the code for DropShadowEffect should have to be worked.

I'm wondering why this is happening.

Thank you !

XAML code added-

<Grid x:Name="Grid_Timeline" ScrollViewer.VerticalScrollBarVisibility="Auto" UseLayoutRounding="True" Width="1159" HorizontalAlignment="Left" VerticalAlignment="Top" SnapsToDevicePixels="True">
</Grid>

Minimal code to re-produce is added-

private void Window_Loaded(object sender, RoutedEventArgs e)
{
        int count_each_category = 0;
        string current_therapeutic_category = String.Empty;

        foreach (DataRow dr_test in dt.Rows)
        {
            if (dr_test != null)
            {
                if (current_category == String.Empty)
                {
                    count_each_category++;

                    current_category = dr_test["category"].ToString();
                }
                else
                {
                    if (current_category == dr_test["category"].ToString())
                    {
                        // empty
                    }
                    else
                    {
                        count_each_category++;

                        current_category = dr_test["category"].ToString();
                    }
                }

                Rectangle rectangle_test = new Rectangle();

                rectangle_test.HorizontalAlignment = HorizontalAlignment.Left;
                rectangle_test.VerticalAlignment = VerticalAlignment.Top;

                rectangle_test.Margin = new Thickness(119 + (((Convert.ToDateTime(dr_test["date"]) - DateTime.Today.AddYears(-10)).TotalDays) * 0.27), count_each_category * 50, 0, 0);

                rectangle_test.Width = 0.27 * Convert.ToInt32(dr_test["howlong"]);
                rectangle_test.Height = 19;

                rectangle_test.Effect = new DropShadowEffect
                {
                    Color = new Color { A = 255, R = 0, G = 0, B = 0 },
                    Direction = 315,
                    ShadowDepth = 5,
                    Opacity = 1
                };

                rectangle_test.Fill = Brushes.LightGreen;

                Grid_Timeline.Children.Add(rectangle_test);
            }
        }
}

XAML code of WPF Window

<Window x:Class="OperationSWdummy.Green_Timeline"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:OperationSWdummy"
    mc:Ignorable="d"
    Title="Green_Timeline" Width="1165" Background="White" ResizeMode="CanMinimize" ScrollViewer.VerticalScrollBarVisibility="Auto" UseLayoutRounding="True" SnapsToDevicePixels="True" Loaded="Window_Loaded">
<Grid x:Name="Grid_Timeline" ScrollViewer.VerticalScrollBarVisibility="Auto" UseLayoutRounding="True" Width="1159" HorizontalAlignment="Left" VerticalAlignment="Top" SnapsToDevicePixels="True">
</Grid>
</Window>

Raw data of dt (DataTable)

date        datetime

category    NVARCHAR

howlong     int

   date     category howlong 
2015-01-25    HHH      60 
2014-04-03    AAA      60 
2015-01-25    DDD      60 
2014-04-03    UUU      60 
2015-01-25    CCC      60 
2015-11-07    PPP      30 
2015-01-25    TTT      60 
2015-11-07    MMM      30 
2015-02-22    MMM      30 
2015-11-07    VVV       8 

Result of above minimal code Result of above minimal code

Another minimal code to create rectangles randomly-

for (int k = 0; k < 191; k++)
        {
            Rectangle rec_test = new Rectangle();
            rec_test.Margin = new Thickness(random_margin.Next(100, 1000), 29 * (k % 10), 0, 0);
            rec_test.Width = random_width.Next(10, 40);
            rec_test.HorizontalAlignment = HorizontalAlignment.Left;
            rec_test.VerticalAlignment = VerticalAlignment.Top;
            rec_test.Height = 14;
            rec_test.Cursor = Cursors.Hand;
            rec_test.Effect = new DropShadowEffect
            {
                Color = new Color { A = 255, R = 0, G = 0, B = 0 },
                Direction = 315,
                ShadowDepth = 5,
                Opacity = 1
            };

            rec_test.Fill = Brushes.Green;

            Grid_Timeline.Children.Add(rec_test);
        }
Eriawan Kusumawardhono
  • 4,796
  • 4
  • 46
  • 49
Kay Lee
  • 922
  • 1
  • 12
  • 40
  • Can you share your xaml? Is Grid_Timeline is WPF Grid? – G K Oct 06 '18 at 16:11
  • @GK, Hello, here is now morning in Far-East Asia. I added xaml code of the grid. I checked whenever I run this (case 1), same yellow rectangles are always without shadow meaning the problem is re-produced. When I tested case 2, case 3, there're also always same rectangles without shadow. I think there might be some funtionality for better performance of WPF like virtualization functionality of stackpanel. Drawing too many rectangles is the reason? – Kay Lee Oct 07 '18 at 03:00
  • I did tested with the code except I used for loop up-to 10. I am able to get rectangle printed with shadow effect. – G K Oct 07 '18 at 05:26
  • Is the code is same as what the image reflects? I have not seeing any position defined in your foreach loop to print the rectangles accordingly. – G K Oct 07 '18 at 05:28
  • @GK, this is prescription data like 19th November, 2017 anti-hypertension medicine for 30 days and 4th April, 2018 pain-killer for 7 days and so on. In aspect of computer processes, it will be just same if we draw hundreds rectangles with random locations for really sure. In the above image, there're around 100 rectangles which many of them look as one rectangle together. – Kay Lee Oct 07 '18 at 08:27
  • You probably have multiple rectangles placed on top of each other. BTW, your code does not show how you do their placement. The drop shadow effect itself [seems to be working flawlessly](https://imgur.com/0f3iUUO). As a test, add margin to your rectangles and place them on a wrap panel. – jsanalytics Oct 10 '18 at 13:33
  • @jsanalytics, Thanks for your precious comment & time. If so, a shadow of Top rectangle should be seen. The sky & green rectangles are many and placed on top of each other but shadows can be seen obviously. I'm using Wrappanel in my application but cannot understand why you mention it. – Kay Lee Oct 11 '18 at 03:26
  • Please add a full reproducing code. – Simon Mourier Oct 11 '18 at 06:38
  • @KayLee I mentioned wrap panel because it would take care of not overlapping rectangles (see picture in my previous comment). Your code is NOT [MCVE](https://stackoverflow.com/help/mcve), which makes it hard for anyone to help. – jsanalytics Oct 11 '18 at 09:01
  • @GK, Hello, The minimal code to re-produce is added to original post. I confirm eventhough this only has 10 rectangles, there're always some rectangles without shadow. However, strangely, If I create rectangles randomly with also added at the last of original post, the randomly created rectangles all have shadow. I tested several times. Are there some relationship with my computer system ? FYI, My system is, NET Framework 4.7.2 Windows 7 Ultimate 8 GB 64 bit Display adaptor- Intel(R) HD Graphics 520 Visual Studio Enterprise 2017 15.8.6 – Kay Lee Oct 11 '18 at 12:45
  • 1
    @jsanalytics, please kindly refer to my latest comment to GK and edited original post. Thank you ! – Kay Lee Oct 11 '18 at 12:46
  • @SimonMourier, please kindly refer to my latest comment to GK and edited original post. Thank you ! – Kay Lee Oct 11 '18 at 12:47
  • @KayLee, [The problem did not reproduce](https://imgur.com/OQYdawb) on Windows 10, .NET 4.6.1 – jsanalytics Oct 11 '18 at 20:09
  • @jsanalytics, manymany thanks for your care and time. As of the answer by l33t, I assume Dropshadoweffect is not just simple, ordinary function of WPF.. – Kay Lee Oct 12 '18 at 02:55
  • Update your graphics driver – GazTheDestroyer Oct 12 '18 at 13:40
  • @GazTheDestroyer, As I commented lastly to the answers, I also thought it's related to rendering, video driver but it's just working mysteriously.....Thanks for your comment. – Kay Lee Oct 13 '18 at 04:03
  • Can you provide a project that compile that you can upload somewhere (like github)? – Simon Mourier Oct 14 '18 at 08:04
  • @SimonMourier, truthfully, mysteriously, it's just working..but do you have specific interest? My project is going on and busy. If you really need it, please wait around this weekend.. – Kay Lee Oct 15 '18 at 11:48
  • No it was just to help. If this is all over, you should answer yourself – Simon Mourier Oct 15 '18 at 14:40
  • Honestly, the problem came back and is continuosly being re-produced on my dev. machine again and on several clients' machine. When I have enough time in the future, I'll ask again in this thread with full re-producible code and small backdata. Thank you ! – Kay Lee Nov 01 '18 at 03:22

2 Answers2

5

Have you tried to use Canvas to place your rectangles?

Having Canvas to place your shapes such as rectangles, circles, or even graphical paths with random locations is better than placing on fixed layout container such as Grid or StackPanel.

It is quite known that if you just placed shapes with effects such as DropShadow on fixed layout (with arbitrary X,Y locations) such as Grid and StackPanel, your shapes may be clipped. Because the shapes edges will always be checked for pixel boundaries at render time, and often this is caused by overlapped rendering calculations when it tries to recalculate edges of fixed layout controls.

Eriawan Kusumawardhono
  • 4,796
  • 4
  • 46
  • 49
  • Many thanks for your insightful knowledgement. I've been busy and now I just run as before without a Canvas (with Grid), much mysteriously, it's just working on a Grid with all shadows....I just updated Visual Studio few days ago and done nothing.. – Kay Lee Oct 13 '18 at 04:04
  • Not really. If you go back to use `Grid`, then your shapes' drop shadow effect might be clipped by the grid's row and column boundary. – Eriawan Kusumawardhono Oct 15 '18 at 08:19
2

Could be a driver problem with your graphics hardware. Try disabling hardware acceleration and see if it helps.

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
         RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
    }
}

In general I advise against using the DropShadowEffect as it will have a significantly negative impact on rendering performance.

l33t
  • 18,692
  • 16
  • 103
  • 180
  • Thanks for your comment. I saw about this and tested before and now again I've tested with this at App class exactly but no luck....I'm adding minimal code to re-produce in few minutes. – Kay Lee Oct 11 '18 at 12:28
  • as I commented to another answer, much mysteriously, it's just working with all shadows. I confirm I obviously removed 'RenderOption' code from App class at this time. I thought this is related to rendering, vidio driver but it's just working mysteriously........anyway, thank you so much ! – Kay Lee Oct 13 '18 at 04:01