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

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

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

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

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

WPF

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

Q&A

解決済

1回答

3048閲覧

WPFにて、オブジェクトの並び替えをドラッグアンドドロップで行いたい。

pochikou

総合スコア7

C#

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

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

WPF

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

0グッド

1クリップ

投稿2019/10/23 07:20

前提・実現したいこと

円を3つ並べ、それぞれをドラッグアンドドロップすることで順番を入れ替えられるようにしようとしています。
その際、入れ替えられる円(図の例の場合中央にあった円)は自動で入れ替え後の位置に移動するようにします。
また、自動で入れ替えられる際、円がスライドするようアニメーションをつけています。
イメージ説明

発生している問題・エラーメッセージ

入れ替えが完了後、アニメーションによって動かされた円をドラッグしようとしても対象の円がドラッグに追随してくれません。
(図の例の場合、入れ替え後の1番左の円をドラッグしようとしても反応しません。)
アニメーションをつけた状態で正常にドラッグできるようにするにはどのようにしたらよいか助言をいただきたいです。

該当のソースコード

C#

1using System; 2using System.Collections.Generic; 3using System.Windows.Controls.Primitives; 4using System.Linq; 5using System.Text; 6using System.Windows; 7using System.Windows.Controls; 8using System.Windows.Documents; 9using System.Windows.Input; 10using System.Windows.Media; 11using System.Windows.Media.Animation; 12using System.Windows.Navigation; 13using System.Windows.Shapes; 14 15namespace WpfTest 16{ 17 /// <summary> 18 /// MainWindow.xaml の相互作用ロジック 19 /// </summary> 20 public partial class MainWindow : Window 21 { 22 LayoutManager manager; 23 24 public MainWindow() 25 { 26 InitializeComponent(); 27 manager = new LayoutManager(100.0); 28 29 manager.Add(thumb); 30 manager.Add(thumb2); 31 manager.Add(thumb3); 32 33 34 } 35 private void thumb_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e) 36 { 37 Thumb t = (Thumb)sender; 38 Canvas.SetLeft(t, manager.GetLeft(t)); 39 } 40 41 private void thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) 42 { 43 Thumb t = (Thumb)sender; 44 45 double currentLeft = Canvas.GetLeft(t) + e.HorizontalChange; 46 47 Canvas.SetLeft(t, currentLeft); 48 49 UIElement ui = manager.UpdatePosition(t, currentLeft); 50 51 if (ui != null) 52 { 53 //以下3行をコメントアウトすると問題なく動作する。 54 Duration duration = new Duration(TimeSpan.FromMilliseconds(200)); 55 DoubleAnimation anime = new DoubleAnimation(manager.GetLeft(t), manager.GetLeft(ui), duration); 56 ui.BeginAnimation(Canvas.LeftProperty, anime); 57 58 Canvas.SetLeft(ui, manager.GetLeft(ui)); 59 } 60 } 61 62 } 63 64 //******************************************************************************** 65 // 66 // 以下、どの円を何番目に配置するかを管理するクラスになります 67 // 68 //******************************************************************************** 69 70 71 72 public class LayoutManager 73 { 74 double objectwidth; 75 List<TargetInfo> list; 76 77 public LayoutManager(double width) 78 { 79 objectwidth = width; 80 list = new List<TargetInfo>(); 81 } 82 83 84 public class TargetInfo 85 { 86 public double left; 87 public UIElement element; 88 89 public TargetInfo(double left, UIElement element) 90 { 91 this.left = left; 92 this.element = element; 93 } 94 } 95 96 public void Add(UIElement element) 97 { 98 list.Add(new TargetInfo(list.Count * objectwidth, element)); 99 } 100 101 public double GetLeft(UIElement element) 102 { 103 104 return list.Find(p => p.element.Equals(element)).left; 105 106 } 107 108 public UIElement UpdatePosition(UIElement element, double dCurrent) 109 { 110 if (list.Count > 1) { 111 for (int i = 0; i < list.Count(); i++) 112 { 113 if (list[i].element.Equals(element)) 114 { 115 //右隣と左隣と比較 116 117 //左端だった場合 118 if (i <= 0) 119 { 120 if (list[1].left < dCurrent) 121 { 122 UIElement tmp = list[0].element; 123 list[0].element = list[1].element; 124 list[1].element = tmp; 125 return list[0].element; 126 127 } 128 //右端だった場合 129 } 130 else if (i >= list.Count() - 1) 131 { 132 if (list[i - 1].left > dCurrent) 133 { 134 UIElement tmp = list[i].element; 135 list[i].element = list[i - 1].element; 136 list[i - 1].element = tmp; 137 return list[i].element; 138 } 139 } 140 else 141 { 142 if (list[i - 1].left > dCurrent) 143 { 144 UIElement tmp = list[i].element; 145 list[i].element = list[i - 1].element; 146 list[i - 1].element = tmp; 147 return list[i].element; 148 149 }else if(list[i + 1].left < dCurrent) 150 { 151 UIElement tmp = list[i].element; 152 list[i].element = list[i + 1].element; 153 list[i + 1].element = tmp; 154 return list[i].element; 155 156 } 157 158 } 159 } 160 } 161 } 162 return null; 163 } 164 } 165} 166 167 168 169 170

XAML

1<Window x:Class="WpfTest.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfTest" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="450" Width="800" 9 > 10 <Canvas Name="canvas" Background="AliceBlue"> 11 <Thumb Name="thumb" DragCompleted="thumb_DragCompleted" DragDelta="thumb_DragDelta" Canvas.Top="0" Canvas.Left="0"> 12 <Thumb.Template> 13 <ControlTemplate> 14 <Ellipse Name="ellipse" Fill="LightBlue" 15 Width="90" Height="90"/> 16 17 </ControlTemplate> 18 </Thumb.Template> 19 </Thumb> 20 <Thumb Name="thumb2" DragCompleted="thumb_DragCompleted" DragDelta="thumb_DragDelta" Canvas.Top="0" Canvas.Left="101"> 21 <Thumb.Template> 22 <ControlTemplate> 23 <Ellipse Name="ellipse" Fill="Aquamarine" 24 Width="90" Height="90"/> 25 26 </ControlTemplate> 27 </Thumb.Template> 28 </Thumb> 29 <Thumb Name="thumb3" DragCompleted="thumb_DragCompleted" DragDelta="thumb_DragDelta" Canvas.Top="0" Canvas.Left="201"> 30 <Thumb.Template> 31 <ControlTemplate> 32 <Ellipse Name="ellipse" Fill="Bisque" 33 Width="90" Height="90"/> 34 35 </ControlTemplate> 36 </Thumb.Template> 37 </Thumb> 38 </Canvas> 39</Window> 40 41

試したこと

アニメーションを除外すると、どの円をドラッグしても問題なく追随してくれます。

具体的には、添付しましたソースコード中の以下コメントがある箇所になります。

//以下3行をコメントアウトすると問題なく動作する。

補足情報(FW/ツールのバージョンなど)

開発環境
VS2019

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

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

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

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

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

Zuishin

2019/10/23 12:21

ドラッグ中にアニメーションしようとしているようですが、ドロップ後にしたらいいのではありませんか?
pochikou

2019/10/24 00:31

ご指摘有難うございます。 DragCompletedの中で一括でアニメーションをするよう変更してみましたが、同様の現象が出てしまいました。
guest

回答1

0

自己解決

自己解決しました。

以下のようにドラッグ終了後に各オブジェクトに対してBeginAnimationの第2引数をnullにして実行したところ想定通りの動作をするようになりました。

C#

1 foreach (var ui in manager.list) 2 { 3 ui.element.BeginAnimation(Canvas.LeftProperty, null); 4 }

投稿2019/10/26 05:18

pochikou

総合スコア7

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問