티스토리 뷰

XAML 페이지에서 스토리보드를 만들고 실행 시킬 수 있지만.
비하인드 코드에서 동적으로 스토리보드를 생성해야 하는 경우가 있다.

아래코드를 참조해서 한번 실행 해 보면 된다.


namespace Bing.ChartType
{
    public partial class Line : UserControl
    {
        private List<double> _value1 = new List<double>();
        private List<double> _value2 = new List<double>();
        private double MaxValue;
        private double MinValue;
        private double differValue;
        private double MaxHeight = 130;
        private double dewidth = 20;
        Storyboard sb = new Storyboard(); 

        public Line()
        {
            InitializeComponent();  
        }

        private void CreatePanel()
        { 
            StackPanel sValueText = new StackPanel();
            sValueText.Orientation = Orientation.Horizontal;
            sValueText.VerticalAlignment = VerticalAlignment.Center;
            sValueText.HorizontalAlignment = HorizontalAlignment.Center;
            sValueText.Width = 90;
            xChartArea.Children.Add(sValueText); 
            sValueText.SetValue(Grid.RowProperty, 1);

        }

        private void Initialize()
        {
            MaxValue = Convert.ToDouble(_value1.Max());
            MinValue = Convert.ToDouble(_value1.Min());  

            int duration = 2; 

            for (int i = 0; i < _value1.Count; i++)
            {
                  Grid _grid = new Grid();
                _grid.Width = 10;
                _grid.Height = 10;
                _grid.Name = "pointGrid" + i;
                _grid.Opacity = 0.0;

                Ellipse elip = new Ellipse();
                elip.Width = 10;
                elip.Height = 10;
                elip.Fill = new SolidColorBrush(Color.FromArgb(100, 226, 226, 226));
                elip.Stroke = new SolidColorBrush(Colors.White);

                Ellipse smallelip = new Ellipse();
                smallelip.Width = 6;
                smallelip.Height = 6;
                smallelip.Fill = new SolidColorBrush(Color.FromArgb(100, 241, 155, 0));
                smallelip.Stroke = new SolidColorBrush(Colors.White);

                _grid.Children.Add(elip);
                _grid.Children.Add(smallelip); 

                xCanvas.Children.Add(_grid);

               //  sb.Duration = new Duration(TimeSpan.FromSeconds(4));

                DoubleAnimation dAnimation = new DoubleAnimation();

                dAnimation.Duration = new Duration(TimeSpan.FromSeconds(duration));

                Storyboard.SetTargetName(dAnimation, ((Grid)xCanvas.Children[i]).Name);
                Storyboard.SetTargetProperty(dAnimation, new PropertyPath("Opacity"));

                sb.Children.Add(dAnimation); 
                
                //da.From = 0.0;
                dAnimation.To = 1.0;  
                 
                duration += 2; 
            }

            LayoutRoot.Resources.Remove("storybd");
            LayoutRoot.Resources.Add("storybd", sb); 

            sb.RepeatBehavior = RepeatBehavior.Forever;
        
        }

        private void SetValue()
        {
            MaxValue = Convert.ToDouble(_value1.Max());
            MinValue = Convert.ToDouble(_value1.Min()); 
            differValue = MaxValue - MinValue;

            double widthValue = 10;

            for(int i = 0; i < _value1.Count; i++)
            {   
               xCanvas.Children[i].SetValue(Canvas.LeftProperty, widthValue);
               xCanvas.Children[i].SetValue(Canvas.TopProperty, MaxHeight - ((MaxHeight / (MaxValue - MinValue)) * ( _value1[i] - MinValue)) );

                widthValue += 60;
            }  
        }

        public List<double> Value1
        {
            get { return _value1; }
            set 
            { 
                _value1 = value; 
                Initialize();
                SetValue();
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            sb.Begin();  
        }  
    } 
}


Version 2 

 Storyboard storyBar = new Storyboard();

                    DoubleAnimation heightAnimation = new DoubleAnimation() { Duration = new Duration(TimeSpan.FromMilliseconds(700)), From = 0.0, To = FirstBarHeight };
                    
                    DoubleAnimationUsingKeyFrames heightFrame = new DoubleAnimationUsingKeyFrames();
                    heightFrame.KeyFrames.Add(new EasingDoubleKeyFrame(){ KeyTime = TimeSpan.FromMilliseconds(700), Value = 0});
                    heightFrame.KeyFrames.Add(new EasingDoubleKeyFrame() { KeyTime = TimeSpan.FromMilliseconds(1400), Value = SecondBarHeight });
                    Storyboard.SetTargetProperty(heightFrame, new PropertyPath(@"(FrameworkElement.Height)"));
                    Storyboard.SetTargetName(heightFrame,recBar2.Name);
                    storyBar.Children.Add(heightFrame);

                    Storyboard.SetTargetName(heightAnimation, recBar.Name);
                    Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(Rectangle.HeightProperty));
                    storyBar.Children.Add(heightAnimation);
                      
this.Resources.Clear();
                    this.Resources.Add("sbDrawBar" + i.ToString(), storyBar);
                     
                    storyBar.Begin();

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함