动态添加许多控件时,有时会丢失DropShadowEffect [英] DropShadowEffect is sometimes missing when dynamically adding many controls

查看:86
本文介绍了动态添加许多控件时,有时会丢失DropShadowEffect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

foreach(dt.Rows中的DataRow dr)
{
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
};

//如你所见,如果添加了一个矩形,

//就应该使用DropShadowEffect代码。


Grid_Timeline .Children.Add(rectangle_timeline);
}

xaml code-

 < Grid x:Name =" Grid_Timeline" ScrollViewer.VerticalScrollBarVisibility = QUOT;自动" UseLayoutRounding = QUOT;真"宽度= QUOT; 1159"的Horizo​​ntalAlignment = QUOT;左" VerticalAlignment = QUOT;陀螺" SnapsToDevicePixels = QUOT;真"> 
< / Grid>

我动态地添加许多带有上述简单代码的矩形,如图所示。

然而,有时,随机地,没有DropShadowEffect的矩形像黄色矩形和最低的1个蓝色矩形。

如您所见代码,如果是添加了矩形,必须使用DropShadowEffect的代码。

当我测试案例2,案例3时,总是有相同(特定)的矩形没有阴影。

我认为可能有一些功能可以提高WPF的性能,比如stackpanel的虚拟化功能。

大约有100个矩形,其中许多看起来像一个矩形。

绘制太多的矩形是什么原因?

我想知道为什么会这样。

谢谢!

添加了重新生成的最小代码 -

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

foreach(DataRow dr_test在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.Horizo​​ntalAlignment = Horizo​​ntalAlignment.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);
}
}
}

  WPF窗口的XAML代码

< 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"宽度= QUOT; 1165"背景= QUOT;白色" ResizeMode = QUOT; CanMinimize" ScrollViewer.VerticalScrollBarVisibility = QUOT;自动" UseLayoutRounding = QUOT;真" SnapsToDevicePixels = QUOT;真"装载= QUOT; Window_Loaded">
< Grid x:Name =" Grid_Timeline" ScrollViewer.VerticalScrollBarVisibility = QUOT;自动" UseLayoutRounding = QUOT;真"宽度= QUOT; 1159"的Horizo​​ntalAlignment = QUOT;左" VerticalAlignment = QUOT;陀螺" SnapsToDevicePixels = QUOT;真">
< / Grid>
< / Window>

dt的原始数据(DataTable)



日期&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;日期时间


类别    NVARCHAR


多久  &NBSP;&NBSP; int































< tr height ="22"style ="height:16.5pt">






























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



以上最小代码的结果 -



另一个随机创建矩形的最小代码 -

 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.Horizo​​ntalAlignment = Horizo​​ntalAlignment.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);
}


解决方案

嗨kay_Lee,


根据你的描述和代码,你当你动态添加许多矩形时,掉落阴影效果会消失。


但我使用你的代码,我无法在你身边重现你的问题,什么是dt?并且矩形不会显示在网格中,因为您没有为这些矩形填充任何颜色。



请发布整个样本可以重现这个问题,这有助于我们理解和重现您的问题。



最好的问候,


樱桃


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 }; // as you see, if a rectangle is added,

// the just above DropShadowEffect code should be worked.

Grid_Timeline.Children.Add(rectangle_timeline); }

xaml code-

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

I dynamically add many Rectangles 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.

When I tested case 2, case 3, there're also always same(specific) rectangles without shadow.

I think there might be some funtionality for better performance of WPF like virtualization functionality of stackpanel.

There're around 100 rectangles which many of them look as one rectangle together.

Drawing too many rectangles is the reason?

I'm wondering why this is happening.

Thank you !

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);
                }
            }
}

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

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);
            }

解决方案

Hi kay_Lee,

According to your description and code, you said that theDropshadoweffect will disappear when you add many rectangles dynamically.

But I use your code, I can not reproduce your issue at my side, what is dt? And the rectangle don't display in Grid, because you don't fill any color for these rectangles.

Please post the entire sample that can reproduce this issue, it is helpful for us to understand and reproduce your issue.

Best Regards,

Cherry


这篇关于动态添加许多控件时,有时会丢失DropShadowEffect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆