質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

1回答

663閲覧

viewとviewModelの連携

yisland

総合スコア28

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

1クリップ

投稿2017/04/15 01:36

参考サイト

参考サイトを見て仮想化しないでも速い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}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ItemsControlを使う方法がありますね。

http://stackoverflow.com/questions/7177432/how-to-display-items-in-canvas-through-binding

あとは、UserContorolからDependencyProperty を使って、Bindingした内容を操作するコードを書くということですかね。

投稿2017/04/15 11:43

kiichi54321

総合スコア1984

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問