Click here to Skip to main content
15,886,058 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how correctly move this code from code behind to mvvm?
namespace DemoApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ChartSurface.ChartSurface cs;
        double[] data;
        public MainWindow()
        {

            InitializeComponent();

            this.FontSizeCB.SelectionChanged += new SelectionChangedEventHandler(OnFontSizeCBChanged);
            this.FontFamilyCB.SelectionChanged += new SelectionChangedEventHandler(OnFontFamilyCBChanged);
            this.LineBrashCB.SelectionChanged += new SelectionChangedEventHandler(OnLineBrashCBChanged);
            this.LineThicknessCB.SelectionChanged += new SelectionChangedEventHandler(OnLineThicknessCBChanged);
            this.XAxisTitleTB.TextChanged += new TextChangedEventHandler(OnXAxisTitleTBChanged);
            this.YAxisTitleTB.TextChanged += new TextChangedEventHandler(OnYAxisTitleTBChanged);
            this.SinewaveRB.Checked += new RoutedEventHandler(OnTWaveRBChecked);
            this.SquarewaveRB.Checked += new RoutedEventHandler(OnTWaveRBChecked);

            cs = new ChartSurface.ChartSurface
            {
                LineData = new Double[] { 0, 6, 1, 2, 2, 4.6, 3, 3.5, 4, 1.9, 5, 3, 6, 5, 7, 9, 8, 4, 9, 2, 10, 3.5 },
                LineThickness = 2,
                LineBrush = Brushes.DarkRed,
                FontFamily = "Comic Sans MS",
                FontSize = 15,
                XAxisTitle = this.XAxisTitleTB.Text,
                YAxisTitle = this.YAxisTitleTB.Text
            };
            this.DataContext = cs;
            data = cs.LineData.ToArray();
        }

        private void OnTWaveRBChecked(object sender, RoutedEventArgs e)
        {
            if (this.SinewaveRB.IsChecked == true)
                cs.LineData = data;
            else
                cs.LineData = CreateStepLineSeries(data);
        }

        private void OnYAxisTitleTBChanged(object sender, TextChangedEventArgs e)
        {
            cs.YAxisTitle = (sender as TextBox).Text;
        }

        private void OnXAxisTitleTBChanged(object sender, TextChangedEventArgs e)
        {
            cs.XAxisTitle = (sender as TextBox).Text;
        }

        private void OnLineThicknessCBChanged(object sender, SelectionChangedEventArgs e)
        {
            cs.LineThickness = Convert.ToDouble(((sender as ComboBox).SelectedItem as ComboBoxItem).Content as string);
        }

        private void OnLineBrashCBChanged(object sender, SelectionChangedEventArgs e)
        {
            cs.LineBrush = ((sender as ComboBox).SelectedItem as TextBlock).Background as Brush;
        }

        private void OnFontFamilyCBChanged(object sender, SelectionChangedEventArgs e)
        {
            cs.FontFamily = ((sender as ComboBox).SelectedItem as ComboBoxItem).Content as string;
        }

        private void OnFontSizeCBChanged(object sender, SelectionChangedEventArgs e)
        {
            cs.FontSize = Convert.ToDouble(((sender as ComboBox).SelectedItem as ComboBoxItem).Content as string);
        }

        double[] CreateStepLineSeries(double[] source)
        {
            List<double> returnValue = new List<double>();
            for (int i = 1; i < source.Length; i += 2)
            {
                double currentValueX = source[i - 1];
                double currentValueY = source[i];
                returnValue.Add(currentValueX);
                returnValue.Add(currentValueY);
                if (i < source.Length - 1)
                {
                    double nextValueX = source[i + 1];
                    returnValue.Add(nextValueX);
                    returnValue.Add(currentValueY);
                }
            }
            return returnValue.ToArray();
        }
    }
}


What I have tried:

I tried to move this part into viewmodel class
cs = new ChartSurface.ChartSurface
            {
                LineData = new Double[] { 0, 6, 1, 2, 2, 4.6, 3, 3.5, 4, 1.9, 5, 3, 6, 5, 7, 9, 8, 4, 9, 2, 10, 3.5 },
                LineThickness = 2,
                LineBrush = Brushes.DarkRed,
                FontFamily = "Comic Sans MS",
                FontSize = 15,
                XAxisTitle = this.XAxisTitleTB.Text,
                YAxisTitle = this.YAxisTitleTB.Text
            };


class ViewModel
   {
       ChartSurface.ChartSurface cs;

       public ViewModel()
       {
           cs = new ChartSurface.ChartSurface
           {
               LineData = new Double[] { 0, 6, 1, 2, 2, 4.6, 3, 3.5, 4, 1.9, 5, 3, 6, 5, 7, 9, 8, 4, 9, 2, 10, 3.5 },
               LineThickness = 2,
               LineBrush = Brushes.DarkRed,
               FontFamily = "Comic Sans MS",
               FontSize = 15,
               XAxisTitle = "xx",
               YAxisTitle = "yy"
           };
       }
   }


and change this line in codebehind
this.DataContext = cs;


into this
this.DataContext = new ViewModel();

but it`s doesn't work.
Posted
Updated 13-Jun-18 8:05am

1 solution

Quote:
this.DataContext = cs;
into this
this.DataContext = new ViewModel();
but it`s doesn't work.


The statements you posted are not "equivalent".

Assuming the first statement even worked, your "ChartSurface" is now a "member" of the ViewModel (class); versus before when it was "at the top".

And since ChartSurface is a 3rd party chart, it's (part of) a "view" and not a "view model"; and ideally should have been wrapped in a user control.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900