[英]WPF drag and drop C# drag image as well
我動態地用數據庫的答案填充停靠面板,並用數據庫的問題填充另一個停靠面板。 答案將被填充為“標簽”,而我嘗試將標簽拖放到textblock中。 是的,我可以拖放,但問題是我也想拖動標簽。 例如,如果Label內容為Hello,我也希望用單詞“ hello”拖動問候,現在,當我拖動它時,它也不會拖動該單詞,但是當我將其放入在文本框中,刪除了“你好”一詞。 我也想將動畫或單詞與光標一起拖動。
這是我的代碼:
private void PopulateQuestion(int activityID, int taskID)
{
IList<Model.question> lstQuestion = qn.GetRecords(taskID, activityID);
StackPanel sp = new StackPanel();
StackPanel stp = new StackPanel();
foreach (Model.question qhm in lstQuestion)
{
StackPanel sp1 = new StackPanel() { Orientation = Orientation.Horizontal }; // Question
TextBlock tb = new TextBlock();
tb.Text = qhm.QuestionContent;
tb.FontWeight = FontWeights.Bold;
tb.FontSize = 24;
sp1.Children.Add(tb);
StackPanel sp2 = new StackPanel() { Orientation = Orientation.Horizontal }; // Answer
Label tb1 = new Label();
tb1.Content = qhm.Answer;
tb1.FontWeight = FontWeights.Bold;
tb1.FontSize = 24;
tb1.MouseLeftButtonDown += tb1_Click;
sp2.Children.Add(tb1);
TextBox tbox = new TextBox();
tbox.Width = 100;
tbox.FontSize = 24;
tbox.AllowDrop = true;
tbox.FontWeight = FontWeights.Bold;
if (qhm.Answer.Trim().Length > 0 )
{
sp1.Children.Add(tbox);
}
sp.Children.Add(sp1);
stp.Children.Add(sp2);
}
dockQuestion.Children.Add(sp);
dockAnswer.Children.Add(stp);
}
private void tb1_Click(object sender, RoutedEventArgs e)
{
Label lbl = (Label)sender;
DataObject dataObj = new DataObject(lbl.Content);
DragDrop.DoDragDrop(lbl, dataObj, DragDropEffects.All);
lbl.IsEnabled = false;
lbl.Foreground = (SolidColorBrush)new BrushConverter().ConvertFromString("#FFFB3B46"); // Red
}
您可以按照以下鏈接中概述的策略進行操作,該策略實際上是創建一個新窗口,並導致使用鼠標光標更新窗口位置。
http://blogs.msdn.com/b/jaimer/archive/2007/07/12/drag-drop-in-wpf-explained-end-to-end.aspx
因此,頁面的主要目的是您使用Adorner裝飾光標。
您可以在DragSource事件處理程序上使用this.DragSource.GiveFeedback和其他事件來設置Adorner。
一旦有了事件處理程序,就可以給您做一些事情的機會。
//Here we create our adorner..
_adorner = new DragAdorner(DragScope, (UIElement)this.dragElement, true, 0.5);
_layer = AdornerLayer.GetAdornerLayer(DragScope as Visual);
_layer.Add(_adorner);
因此,您可以通過將其子類化來創建自己的Adorner。 您可以在此處找到有關創建自定義裝飾器的更多信息:
默認的wpf拖放動畫非常丑陋,如果要在拖動時顯示一些文本或圖像,則需要做更多的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.