'How to scroll Listview Item to a next item on click the next Item Programatically Wpf

I am trying to build a scrollable Calendar. I am getting values from a JSON dynamically from which I am using minimum year and maximum year and so on. Below is the code and screenshot I have written but how to scroll the listview on click and develop this feature please help. This is what i tried. 1)First I have create 3 listview and 3 datatemplate

    private UIElement myExperitmentCal(Date maxDate, Date minDate, Boolean includeYear)
            {
                modelfillingMethod(maxDate, minDate, includeYear);
                StackPanel mainStack = new StackPanel();
                mainStack.Orientation = Orientation.Horizontal;
                mainStack.Height = 138;
                mainStack.HorizontalAlignment = HorizontalAlignment.Center;
                mainStack.VerticalAlignment = VerticalAlignment.Top;
    
                ListView myListView1 = new ListView();
                myListView1.BorderThickness = new Thickness(0);
                myListView1.Background = new SolidColorBrush(Colors.Transparent);
                ScrollViewer.SetVerticalScrollBarVisibility(myListView1, ScrollBarVisibility.Disabled);
                ScrollViewer.SetHorizontalScrollBarVisibility(myListView1, ScrollBarVisibility.Disabled);
                ListView myListView2 = new ListView();
                myListView2.BorderThickness = new Thickness(0);
                ScrollViewer.SetVerticalScrollBarVisibility(myListView2, ScrollBarVisibility.Disabled);
                ScrollViewer.SetHorizontalScrollBarVisibility(myListView2, ScrollBarVisibility.Disabled);
                myListView2.Background = new SolidColorBrush(Colors.Transparent);
                ListView myListView3 = new ListView();
                myListView3.BorderThickness = new Thickness(0);
                ScrollViewer.SetVerticalScrollBarVisibility(myListView3, ScrollBarVisibility.Disabled);
                ScrollViewer.SetHorizontalScrollBarVisibility(myListView3, ScrollBarVisibility.Disabled);
                myListView3.Background = new SolidColorBrush(Colors.Transparent);
    
                TextBlock t1 = new TextBlock();
                TextBlock t2 = new TextBlock();
                TextBlock t3 = new TextBlock();
    
                DataTemplate dt1 = new DataTemplate();
                FrameworkElementFactory spFactory1 = new FrameworkElementFactory(typeof(StackPanel));
                spFactory1.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
                spFactory1.SetValue(StackPanel.WidthProperty, (double)100);
                spFactory1.SetValue(StackPanel.HeightProperty, (double)40);
                FrameworkElementFactory cardHolder1 = new FrameworkElementFactory(typeof(TextBlock));
                cardHolder1.SetBinding(TextBlock.TextProperty, new Binding("MonthName"));
                cardHolder1.SetValue(TextBlock.FontSizeProperty, (double)18);
                cardHolder1.SetValue(TextBlock.ForegroundProperty, BixbyViewStyler.BRUSH_TIMEPIKER_FONT);
                cardHolder1.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Center);
                cardHolder1.SetValue(Border.MarginProperty, new Thickness(10, 0, 0, 0));
                spFactory1.AppendChild(cardHolder1);
    
                DataTemplate dt2 = new DataTemplate();
                FrameworkElementFactory spFactory2 = new FrameworkElementFactory(typeof(StackPanel));
                spFactory2.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
                spFactory2.SetValue(StackPanel.WidthProperty, (double)90);
                spFactory2.SetValue(StackPanel.HeightProperty, (double)40);
                FrameworkElementFactory cardHolder2 = new FrameworkElementFactory(typeof(TextBlock));
                cardHolder2.SetBinding(TextBlock.TextProperty, new Binding("NumberOfDays"));
                cardHolder2.SetValue(TextBlock.FontSizeProperty, (double)14);
                cardHolder2.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Center);
                cardHolder2.SetValue(TextBlock.ForegroundProperty, BixbyViewStyler.BRUSH_TIMEPIKER_FONT);
                spFactory2.AppendChild(cardHolder2);
    
                DataTemplate dt3 = new DataTemplate();
                FrameworkElementFactory spFactory3 = new FrameworkElementFactory(typeof(StackPanel));
                spFactory3.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
                spFactory3.SetValue(StackPanel.WidthProperty, (double)90);
                spFactory3.SetValue(StackPanel.HeightProperty, (double)40);
                FrameworkElementFactory cardHolder3 = new FrameworkElementFactory(typeof(TextBlock));
                cardHolder3.SetBinding(TextBlock.TextProperty, new Binding("Year"));
                cardHolder3.SetValue(TextBlock.FontSizeProperty, (double)14);
                cardHolder3.SetValue(TextBlock.ForegroundProperty, BixbyViewStyler.BRUSH_TIMEPIKER_FONT);
                cardHolder3.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Center);
                spFactory3.AppendChild(cardHolder3);
    
                dt1.VisualTree = spFactory1;
                dt2.VisualTree = spFactory2;
                dt3.VisualTree = spFactory3;
    
                myListView1.ItemTemplate = dt1;
                myListView2.ItemTemplate = dt2;
                myListView3.ItemTemplate = dt3;
                myListView1.ItemsSource = monthNamesList;
                myListView1.SelectionChanged += myListView1OnSelectionChanged;
                myListView2.ItemsSource = daysInMonthsdataList;
                myListView2.SelectionChanged += myListView2OnSelectionChanged;
                myListView3.ItemsSource = yearLimitDataList;
                myListView3.SelectionChanged += myListView3OnSelectionChanged;
    
                mainStack.Children.Add(myListView1);
                mainStack.Children.Add(myListView2);
                mainStack.Children.Add(myListView3);
    
                return mainStack;
            }
2)Filling my models and adding it list to bind data on listview

            List<MonthsNamesInYear> monthNamesList = new List<MonthsNamesInYear>();
            List<daysInMonth> daysInMonthsdataList = new List<daysInMonth>();
            List<yearLimitValue> yearLimitDataList = new List<yearLimitValue>();
            MonthsNamesInYear monthNames;
            daysInMonth days = new daysInMonth();
            yearLimitValue year;
       

        public void modelfillingMethod(Date maxDate, Date minDate, Boolean includeYear)
        {
            int startMonth = 4;
            int endMonth = 5;
            int startYear = 2021;
            int endYear = 2023;
            int startDay = 1;
            int endDay = 28;

            //year
            for (int i = startYear; i <= endYear; i++)
            {
                year = new yearLimitValue();
                year.Year = i;
                yearLimitDataList.Add(year);
            }

            //monthnames
            for (int i = 1; i <= 12; i++)
            {
                string mapValue;
                if (map.TryGetValue(i, out mapValue))
                {
                    monthNames = new MonthsNamesInYear();
                    monthNames.MonthName = mapValue;
                    monthNamesList.Add(monthNames);
                }
            }

            for (int i = startYear; i <= endYear; i++)
            {
                for (int j = startMonth; j <= 12; j++)
                {
                    if (j == endMonth + 1 && i == endYear)

                        daysInMonthsdataList.Add(days);
                }
                startMonth = 1;
            }

        }
        public void monthNameMap()
        {
            map = new Dictionary<int, string>();
            map.Add(1, "January");
            map.Add(2, "February");
            map.Add(3, "March");
            map.Add(4, "April");
            map.Add(5, "May");
            map.Add(6, "June");
            map.Add(7, "July");
            map.Add(8, "August");
            map.Add(9, "September");
            map.Add(10, "October");
            map.Add(11, "November");
            map.Add(12, "December");

        }
    }
    public class MonthsNamesInYear : INotifyPropertyChanged
    {
        private string _MonthName;
        public string MonthName
        {
            get { return _MonthName; }
            set
            {
                _MonthName = value;
                NotifyPropertyChnaged("MonthName");
            }

        }
        private void NotifyPropertyChnaged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
    public class daysInMonth : INotifyPropertyChanged
    {

        private int _NumberOfDays;
        public int NumberOfDays
        {
            get { return _NumberOfDays; }
            set
            {
                _NumberOfDays = value;
                NotifyPropertyChnaged("NumberOfDays");
            }

        }
        private void NotifyPropertyChnaged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
    public class yearLimitValue : INotifyPropertyChanged
    {

        private int _Year;
        public int Year
        {
            get { return _Year; }
            set
            {
                _Year = value;
                NotifyPropertyChnaged("Year");
            }

        }
        private void NotifyPropertyChnaged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }

Below are the images Attached:

What I want to achieve

What I have achieved



Solution 1:[1]

I am finally able to solve this issue using grid Below is the code if anyone else is looking for this solution.

    public class DatePickerClass 
    {
            TextBlock monthPresent = new TextBlock();
            TextBlock monthPast = new TextBlock();
            TextBlock monthFuture = new TextBlock();
            TextBlock datePresent = new TextBlock();
            TextBlock datePast = new TextBlock();
            TextBlock dateFuture = new TextBlock();
            TextBlock yearPast = new TextBlock();
            TextBlock yearPresent = new TextBlock();
            TextBlock yearFuture = new TextBlock();
            Button clickButton = new Button();
            Date maxDate;
            Date minDate;
    
            private UIElement myDatePicker(Date maxDate, Date minDate)
            {
                Grid grid = new Grid();
                grid = setDatePickerColumnRow(grid);
                setDatePickerValue();
                monthPast.MouseLeftButtonDown += monthPast_PreviewMouseDown;
                monthFuture.MouseLeftButtonDown += monthFuture_PreviewMouseDown;
                datePast.MouseLeftButtonDown += datePast_PreviewMouseDown;
                dateFuture.MouseLeftButtonDown += dateFuture_PreviewMouseDown;
                yearFuture.MouseLeftButtonDown += yearFuture_PreviewMouseDown;
                yearPast.MouseLeftButtonDown += yearPast_PreviewMouseDown;
                clickButton.Click += button_click;
                grid.HorizontalAlignment = HorizontalAlignment.Center;
                return grid;
             }
          private void setDatePickerValue()
          {
            //applyDatePickerStyle(); some styles applied
            monthPresent.Text = getmonthName(minDate.getMonth() + 1);
            monthPast.Text = " ";
            monthFuture.Text = getmonthName(minDate.getMonth() + 2);
            yearFuture.Text = (minDate.getYear() + 1).ToString();
            yearPast.Text = "";
            yearPresent.Text = minDate.getYear().ToString();
            dateFuture.Text = (minDate.getDay() + 1).ToString();
            datePast.Text = "";
            datePresent.Text = minDate.getDay().ToString();
        }
        private Grid setDatePickerColumnRow(Grid grid)
        {
            ColumnDefinition month = new ColumnDefinition();
            month.Width = new GridLength(85);
            ColumnDefinition date = new ColumnDefinition();
            date.Width = new GridLength(60);
            ColumnDefinition year = new ColumnDefinition();
            year.Width = new GridLength(60);
            ColumnDefinition colon = new ColumnDefinition();
            colon.Width = new GridLength(27);
            grid.ColumnDefinitions.Add(month);
            grid.ColumnDefinitions.Add(colon);
            grid.ColumnDefinitions.Add(date);
            grid.ColumnDefinitions.Add(year);
            RowDefinition past = new RowDefinition();
            RowDefinition present = new RowDefinition();
            RowDefinition future = new RowDefinition();
            RowDefinition button = new RowDefinition();
            past.Height = new GridLength(60);
            present.Height = new GridLength(60);
            future.Height = new GridLength(60);
            button.Height = new GridLength(80);
            grid.RowDefinitions.Add(past);
            grid.RowDefinitions.Add(present);
            grid.RowDefinitions.Add(future);
            grid.RowDefinitions.Add(button);
            Grid.SetRow(monthPast, 0);
            Grid.SetColumn(monthPast, 0);
            grid.Children.Add(monthPast);
            Grid.SetRow(monthPresent, 1);
            Grid.SetColumn(monthPresent, 0);
            grid.Children.Add(monthPresent);
            Grid.SetRow(monthFuture, 2);
            Grid.SetColumn(monthFuture, 0);
            grid.Children.Add(monthFuture);
            Grid.SetRow(datePast, 0);
            Grid.SetColumn(datePast, 2);
            grid.Children.Add(datePast);
            Grid.SetRow(datePresent, 1);
            Grid.SetColumn(datePresent, 2);
            grid.Children.Add(datePresent);
            Grid.SetRow(dateFuture, 2);
            Grid.SetColumn(dateFuture, 2);
            grid.Children.Add(dateFuture);
            Grid.SetRow(yearPast, 0);
            Grid.SetColumn(yearPast, 3);
            grid.Children.Add(yearPast);
            Grid.SetRow(yearPresent, 1);
            Grid.SetColumn(yearPresent, 3);
            grid.Children.Add(yearPresent);
            Grid.SetRow(yearFuture, 2);
            Grid.SetColumn(yearFuture, 3);
            grid.Children.Add(yearFuture);
            Grid.SetRow(clickButton, 3);
            Grid.SetColumn(clickButton, 1);
            clickButton.SetValue(Grid.ColumnSpanProperty, 3);
            BixbyViewStyler.setTimePickerButtonStyle(ref clickButton);
            grid.Children.Add(clickButton);
            return grid;
        }
        private void monthPast_PreviewMouseDown(object sender, EventArgs e)
        {
            monthPresent.Text = monthPast.Text;
            monthTextChanged();
        }
        private void monthFuture_PreviewMouseDown(object sender, EventArgs e)
        {
            monthPresent.Text = monthFuture.Text;
            monthTextChanged();
        }
        private void datePast_PreviewMouseDown(object sender, EventArgs e)
        {
            datePresent.Text = datePast.Text;
            dateTextChanged();
        }
        private void dateFuture_PreviewMouseDown(object sender, EventArgs e)
        {
            datePresent.Text = dateFuture.Text;
            dateTextChanged();
        }
        private void setPresentDate()
        {
            if (isMaxMonth() && isMaxYear() && (Convert.ToInt32(datePresent.Text.ToString()) > maxDate.getDay()))
                datePresent.Text = maxDate.getDay().ToString();
            else if (isMinMonth() && isMinYear() && (Convert.ToInt32(datePresent.Text.ToString()) < minDate.getDay()))
                datePresent.Text = minDate.getDay().ToString();
            else
                datePresent.Text = datePresent.Text;
        }
        private void dateTextChanged()
        {
            setPresentDate();
            if ((isFirstday() && (!isMaxDate())) || (isMinDate() && (!isLastday())))
            {
                dateFuture.Visibility = Visibility.Visible;
                datePast.Visibility = Visibility.Hidden;
                dateFuture.Text = (Convert.ToInt32(datePresent.Text.ToString()) + 1).ToString();
            }
            else if ((isMaxDate() && (!isFirstday())) || (isLastday() && (!isMinDate())))
            {
                dateFuture.Visibility = Visibility.Hidden;
                datePast.Visibility = Visibility.Visible;
                datePast.Text = (Convert.ToInt32(datePresent.Text.ToString()) - 1).ToString();
            }
            else if ((isMaxDate() && isFirstday()) || (isLastday() && isMinDate()))
            {
                datePast.Visibility = Visibility.Hidden;
                dateFuture.Visibility = Visibility.Hidden;
            }
            else
            {
                datePast.Visibility = Visibility.Visible;
                dateFuture.Visibility = Visibility.Visible;
                dateFuture.Text = (Convert.ToInt32(datePresent.Text.ToString()) + 1).ToString();
                datePast.Text = (Convert.ToInt32(datePresent.Text.ToString()) - 1).ToString();
            }
        }
        private bool isFirstday()
        {
            return (datePresent.Text.ToString().Equals("1"));
        }
        private bool isLastday()
        {
            return getNumberofDays(yearPresent.Text.ToString(), monthPresent.Text.ToString()) == Convert.ToInt32(datePresent.Text.ToString());
        }
        private bool isMaxDate()
        {
            return (isMaxDay() && isMaxMonth() && isMaxYear());
        }
        private bool isMaxDay()
        {
            return (Convert.ToInt32(datePresent.Text.ToString()) == maxDate.getDay());
        }
        private bool isMinDay()
        {
            return (Convert.ToInt32(datePresent.Text.ToString()) == minDate.getDay());
        }
        private bool isMinDate()
        {
            return (isMinDay() && isMinYear() && isMinMonth());
        }
        private bool isMinMonth()
        {
            return (getMonthNumber(monthPresent.Text.ToString()) == (minDate.getMonth() + 1));
        }
        private bool isMaxYear()
        {
            return (Convert.ToInt32(yearPresent.Text.ToString()) == maxDate.getYear());
        }
        private bool isMaxMonth()
        {
            return (getMonthNumber(monthPresent.Text.ToString()) == (maxDate.getMonth() + 1));
        }
        private bool isMinYear()
        {
            return (Convert.ToInt32(yearPresent.Text.ToString()) == minDate.getYear());
        }
        private int getMonthNumber(string monthName)
        {
            return DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture).Month;
        }
        private int getNumberofDays(string year, string month)
        {
            return DateTime.DaysInMonth(Convert.ToInt32(year), getMonthNumber(month));
        }
        private string getmonthName(int monthNumber)
        {
            return CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNumber);
        }
        private string getPreviousMonth(string presentMonth)
        {
            return DateTime.ParseExact(presentMonth, "MMMM", CultureInfo.CurrentCulture).AddMonths(-1).ToString("MMMM");
        }
        private string getNextMonth(string presentMonth)
        {
            return DateTime.ParseExact(presentMonth, "MMMM", CultureInfo.CurrentCulture).AddMonths(+1).ToString("MMMM");
        }
        private void yearPast_PreviewMouseDown(object sender, EventArgs e)
        {
            yearPresent.Text = yearPast.Text;
            yearTextChanged();
        }
        private void yearTextChanged()
        {
            if (isMaxYear())
            {
                yearFuture.Visibility = Visibility.Hidden;
                yearPast.Visibility = Visibility.Visible;
                yearPast.Text = (Convert.ToInt32(yearPresent.Text.ToString()) - 1).ToString();
            }
            else if (isMinYear())
            {
                yearFuture.Visibility = Visibility.Visible;
                yearFuture.Text = (Convert.ToInt32(yearPresent.Text.ToString()) + 1).ToString();
                yearPast.Visibility = Visibility.Hidden;
            }
            else
            {
                yearFuture.Visibility = Visibility.Visible;
                yearPast.Visibility = Visibility.Visible;
                yearFuture.Text = (Convert.ToInt32(yearPresent.Text.ToString()) + 1).ToString();
                yearPast.Text = (Convert.ToInt32(yearPresent.Text.ToString()) - 1).ToString();
            }
            AdjustMonth();
            monthTextChanged();
        }
        public void AdjustMonth()
        {
            if (getMonthNumber(monthPresent.Text.ToString()) > maxDate.getMonth() && isMaxYear())
            {
                monthFuture.Visibility = Visibility.Hidden;
                monthPast.Visibility = Visibility.Visible;
                monthPresent.Text = getmonthName(maxDate.getMonth() + 1);
                monthPast.Text = getPreviousMonth(monthPresent.Text.ToString());
            }
            else if (getMonthNumber(monthPresent.Text.ToString()) < minDate.getMonth() && isMinYear())
            {
                monthPresent.Text = getmonthName(minDate.getMonth() + 1);
                monthPast.Visibility = Visibility.Hidden;
                monthFuture.Visibility = Visibility.Visible;
                monthFuture.Text = getNextMonth(monthPresent.Text.ToString());
            }
            else
            { }
        }
        private void yearFuture_PreviewMouseDown(object sender, EventArgs e)
        {
            yearPresent.Text = yearFuture.Text;
            yearTextChanged();
        }
        private void monthTextChanged()
        {
            if ((getMonthNumber(monthPresent.Text.ToString()) == 12) || (isMaxYear() && isMaxMonth()))
            {
                monthFuture.Visibility = Visibility.Hidden;
                monthPast.Visibility = Visibility.Visible;
                monthPast.Text = getPreviousMonth(monthPresent.Text.ToString());
            }
            else if ((getMonthNumber(monthPresent.Text.ToString()) == 1) || (isMinYear() && isMinMonth()))
            {
                monthPast.Visibility = Visibility.Hidden;
                monthFuture.Visibility = Visibility.Visible;
                monthFuture.Text = getNextMonth(monthPresent.Text.ToString());
            }
            else
            {
                monthFuture.Visibility = Visibility.Visible;
                monthPast.Visibility = Visibility.Visible;
                monthFuture.Text = getNextMonth(monthPresent.Text.ToString());
                monthPast.Text = getPreviousMonth(monthPresent.Text.ToString());
            }
            dateTextChanged();
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RisingDeveloper