参考サイトを見て仮想化しないでも速いCanvasを実装したのですが、
viewとviewModelの連携をどうすれば良いか分かりません。
viewModelからcanvasを操作する方法が分かる方がいらしたら教えてください。
c#
1<Window x:Class="FastCanvas.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:local="clr-namespace:FastCanvas" 5 Title="MainWindow"> 6 <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible"> 7 <local:Canvas x:Name="_canvas" Width="3200" Height="32000"></local:Canvas> 8 </ScrollViewer> 9</Window>
c#
1using System.Windows; 2using System.Windows.Media; 3using System.ComponentModel; 4using System.Collections.ObjectModel; 5 6namespace FastCanvas 7{ 8 public class Canvas : FrameworkElement 9 { 10 public static Point GetLocation(DependencyObject obj) 11 { 12 return (Point)obj.GetValue(LocationProperty); 13 } 14 15 public static void SetLocation(DependencyObject obj, Point value) 16 { 17 obj.SetValue(LocationProperty, value); 18 } 19 public static readonly DependencyProperty LocationProperty = 20 DependencyProperty.RegisterAttached("Location", typeof(Point), typeof(Canvas), 21 new FrameworkPropertyMetadata(default(Point),FrameworkPropertyMetadataOptions.AffectsArrange)); 22 23 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 24 public ObservableCollection<UIElement> Children{get; private set;} 25 26 public Canvas() 27 { 28 Children = new ObservableCollection<UIElement>(); 29 30 Children.CollectionChanged += Children_CollectionChanged; 31 } 32 33 void Children_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 34 { 35 if(e.OldItems != null) 36 { 37 foreach (UIElement oldItem in e.OldItems) 38 { 39 RemoveVisualChild(oldItem); 40 } 41 } 42 if(e.NewItems != null) 43 { 44 foreach (UIElement newItem in e.NewItems) 45 { 46 AddVisualChild(newItem); 47 } 48 } 49 } 50 51 protected override int VisualChildrenCount 52 { 53 get 54 { 55 return Children.Count; 56 } 57 } 58 59 protected override Visual GetVisualChild(int index) 60 { 61 return Children[index]; 62 } 63 64 protected override Size ArrangeOverride(Size finalSize) 65 { 66 foreach(var child in Children) 67 { 68 var location = GetLocation(child); 69 70 child.Arrange(new Rect(location, child.DesiredSize)); 71 } 72 73 return base.ArrangeOverride(finalSize); 74 } 75 76 protected override Size MeasureOverride(Size availableSize) 77 { 78 foreach (var child in Children) 79 { 80 var fe = child as FrameworkElement; 81 82 if( fe != null ) 83 { 84 child.Measure(new Size(fe.Width,fe.Height)); 85 } 86 } 87 88 return base.MeasureOverride(availableSize); 89 } 90 } 91}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。