Create Virtual RowView WPF | Saatody | Amit Padhiyar
XAML
<Window x:Class="WpfApp23.MainWindow" 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:WpfApp23" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="3*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <ScrollViewer Grid.Column="0" Grid.Row="0" Name="LeftScroll"> <StackPanel> <Grid Name="Left"></Grid> </StackPanel> </ScrollViewer> <Grid Grid.Column="1" Grid.Row="0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="20"/> </Grid.ColumnDefinitions> <StackPanel Name="Right" Grid.Column="0"/> <ScrollBar Name="RightScroll" Grid.Column="1"/> </Grid> <Grid Grid.Column="0" Grid.Row="1"> <Button Content="Add Row" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75"/> <TextBox HorizontalAlignment="Left" Height="23" Margin="10,72,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="75"/> <Button Content="Set Height" HorizontalAlignment="Left" Margin="90,72,0,0" VerticalAlignment="Top" Width="75"/> </Grid> <Grid Grid.Column="1" Grid.Row="1"> <Button Content="Add Row" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75"/> <TextBox Name="IndexOf" HorizontalAlignment="Left" Height="23" Margin="10,72,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="75"/> <TextBox Name="Height" HorizontalAlignment="Left" Height="23" Margin="90,72,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="75"/> <Button Name="SetHeight" Content="Set Height" HorizontalAlignment="Left" Margin="170,72,0,0" VerticalAlignment="Top" Width="75"/> </Grid> </Grid> </Window>
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp23 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { private List<Row> Rows = new List<Row>(); private Rows rows = new Rows(); public MainWindow() { InitializeComponent(); Rows = rows.CreateRows(1); System.Console.WriteLine("Rows.Count: " + Rows.Count); RightScroll.Minimum = 0; RightScroll.Maximum = rows.TotalHeight; RightScroll.SmallChange = 10; RightScroll.LargeChange = 30; Event(); } private double Top = 0.0; private int StartRow = 0; private int EndRow = 0; private void Event() { RightScroll.Scroll += (sender, e) => { try { System.Console.WriteLine(RightScroll.Value); System.Console.WriteLine(RightScroll.Minimum); System.Console.WriteLine(RightScroll.Maximum); System.Console.WriteLine(RightScroll.SmallChange); System.Console.WriteLine(RightScroll.LargeChange); Top = 0.0; StartRow = 0; EndRow = 0; double Value = RightScroll.Value; double TotalHeight = 0.0; int Index = 0; while(TotalHeight <= Value) { if (Rows.Count > Index) { TotalHeight += Rows[Index].Height; Index++; } else { break; } } Index -= 1; StartRow = Index; Top = -(Rows[StartRow].Height - (TotalHeight - Value)); TotalHeight = TotalHeight - Value; while (TotalHeight <= Right.ActualHeight) { if (Rows.Count > Index) { TotalHeight += Rows[Index].Height; Index++; } else { break; } } // One More Time if (Rows.Count > Index) { TotalHeight += Rows[Index].Height; Index++; } EndRow = Index - 1; System.Console.WriteLine("Top: " + Top); System.Console.WriteLine("Value: " + RightScroll.Value); System.Console.WriteLine("StartRow: " + StartRow); System.Console.WriteLine("EndRow: " + EndRow); Right.Children.Clear(); for (int i = StartRow; i <= EndRow; i++) { if(i != StartRow) { Top = 0; } RowModel Model = new RowModel(); Model.Row = Rows[i]; Model.Index = Rows[i].Index; Model.Text = Rows[i].Text; Model.Margin = new Thickness(0, Top, 0, 0); Model.Width = Rows[i].Width; Model.Height = Rows[i].Height; Model.Background = new SolidColorBrush(Rows[i].Color); Right.Children.Add(Model); Top += Rows[i].Height; } } catch(Exception Ex) { System.Console.WriteLine("Exception: " + Ex); } }; SetHeight.Click += (sender, e) => { int i = Int32.Parse(IndexOf.Text); double d = Double.Parse(Height.Text); Rows[i].Height = d; double Total = 0.0; for(int loop=0; loop<Rows.Count; loop++) { Total += Rows[loop].Height; } rows.TotalHeight = Total; RightScroll.Maximum = rows.TotalHeight; }; } } public class Row { public int Index { set; get; } public string Text { set; get; } public double Width { set; get; } public double Height { set; get; } public Color Color { set; get; } } public class Rows { private List<Row> rows = new List<Row>(); public double TotalHeight = 0.0; public List<Row> CreateRows(int n) { for (int i = 0; i < n; i++) { Row Row = new Row(); Row.Index = i; Row.Text = "Text-" + i; Row.Width = 1000; Row.Height = 100; TotalHeight += 100; if (i % 2 == 0) { Row.Color = Color.FromRgb(20, 20, 20); } else { Row.Color = Color.FromRgb(40, 40, 40); } rows.Add(Row); } return rows; } } public class RowModel : Grid { public Row Row { set; get; } public int Index { set; get; } private string text; public string Text { set { text = value; TB.Text = text; } get { return Text; } } private TextBlock TB = new TextBlock(); public RowModel() { this.HorizontalAlignment = HorizontalAlignment.Left; this.VerticalAlignment = VerticalAlignment.Top; TB.HorizontalAlignment = HorizontalAlignment.Left; TB.VerticalAlignment = VerticalAlignment.Top; TB.Margin = new Thickness(20, 20, 0, 0); TB.Width = 100; TB.Height = 25; TB.Foreground = new SolidColorBrush(Color.FromRgb(255, 255, 255)); this.Children.Add(TB); } } }
It is work properly with high memory management.
Comments
Post a Comment