Функция перетаскивания в WPFC#

Место общения программистов C#
Ответить
Гость
 Функция перетаскивания в WPF

Сообщение Гость »

Как видите, здесь есть функция перетаскивания. Все работает отлично, но когда я перетаскиваю TrainLeft или TrainRight, я вижу их только внутри ScrollView во время их перемещения. Я хочу видеть TrainLeft и TrainRight за пределами ScrollView, пока я их перемещаю. Я буду использовать StationMap внутри другого окна. Фактически, когда я использую Grid вместо ScrollView, это сработало. Поэтому, когда я использовал этот подход, он сработал, но мне нужно добавить MergeStationTrain автоматически.









































public partial class StationMap : UserControl
{
public ObservableCollection DataList { get; } = new
ObservableCollection();

public MergeStationTrain this[int index]
{
get
{
return DataList[index];
}
set
{
DataList[index] = value;
}
}

public Database database { get; set; } = null;

public StationMap()
{
InitializeComponent();
DataContext = this;
}

public async Task UpdateMap()
{
DataList.Clear();
string query = QueryManager.GetQuery(QueryType.SelectAllStations);
List list = await database.GetTableAsync(query, tableName:
"t_station");
foreach (StationInfo item in list)
{
MergeStationTrain newItem = new MergeStationTrain()
{
StationName = item.stnName,
TrainName = item.stnNo,
ShowLeft = true,
ShowRight = true
};

DataList.Add(newItem);
}
}
}





















public partial class TrainRight : UserControl
{
public static readonly DependencyProperty TrainNameProperty =
DependencyProperty.Register("TrainName", typeof(string), typeof(TrainRight));

public static readonly DependencyProperty ShowFireProperty =
DependencyProperty.Register("ShowFire", typeof(bool), typeof(TrainRight), new PropertyMetadata(false, ShowFirePropertyChanged));

public static readonly DependencyProperty ShowDoorProperty =
DependencyProperty.Register("ShowDoor", typeof(bool), typeof(TrainRight), new PropertyMetadata(false, ShowDoorPropertyChanged));

public bool ShowFire
{
get { return (bool)GetValue(ShowFireProperty); }
set { SetValue(ShowFireProperty, value); }
}

public bool ShowDoor
{
get { return (bool)GetValue(ShowDoorProperty); }
set { SetValue(ShowDoorProperty, value); }
}

public string TrainName
{
get { return (string)GetValue(TrainNameProperty); }
set { SetValue(TrainNameProperty, value); }
}

private ElementAdorner _elementAdorner = default!;

public TrainRight()
{
InitializeComponent();

DataContext = this;

MouseMove += DragControl_MouseMove;
PreviewGiveFeedback += DragControl_PreviewGiveFeedback;
}

private void DragControl_PreviewGiveFeedback(object sender, GiveFeedbackEventArgs e)
{
_elementAdorner.SetArrange(this);
}

private void DragControl_MouseMove(object sender, MouseEventArgs e)
{
AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this);
_elementAdorner = new ElementAdorner(this);
adornerLayer.Add(_elementAdorner);

DragDrop.DoDragDrop(this, this.DataContext, DragDropEffects.Copy);

adornerLayer.Remove(_elementAdorner);
}

private static void ShowFirePropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
TrainRight trainLeft = (TrainRight)d;
bool show = (bool)e.NewValue;

if (show)
{
ImageBehavior.SetAnimatedSource(trainLeft.imFire,
(ImageSource)Application.Current.FindResource("FireGif"));
ImageBehavior.SetRepeatBehavior(trainLeft.imFire, RepeatBehavior.Forever);
}
else
{
ImageBehavior.SetAnimatedSource(trainLeft.imFire, null);
}
}

private static void ShowDoorPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
TrainRight trainLeft = (TrainRight)d;
bool show = (bool)e.NewValue;

if (show)
{
ImageBehavior.SetAnimatedSource(trainLeft.imDoor,
(ImageSource)Application.Current.FindResource("DoorGif"));
ImageBehavior.SetRepeatBehavior(trainLeft.imDoor, RepeatBehavior.Forever);
}
else
{
ImageBehavior.SetAnimatedSource(trainLeft.imDoor, null);
}
}
}

public partial class TrainLeft : UserControl
{
public static readonly DependencyProperty TrainNameProperty =
DependencyProperty.Register("TrainName", typeof(string), typeof(TrainLeft));

public static readonly DependencyProperty ShowFireProperty =
DependencyProperty.Register("ShowFire", typeof(bool), typeof(TrainLeft), new
PropertyMetadata(false, ShowFirePropertyChanged));

public static readonly DependencyProperty ShowDoorProperty =
DependencyProperty.Register("ShowDoor", typeof(bool), typeof(TrainLeft), new
PropertyMetadata(false, ShowDoorPropertyChanged));

public bool ShowFire
{
get { return (bool)GetValue(ShowFireProperty); }
set { SetValue(ShowFireProperty, value); }
}

public bool ShowDoor
{
get { return (bool)GetValue(ShowDoorProperty); }
set { SetValue(ShowDoorProperty, value); }
}

public string TrainName
{
get { return (string)GetValue(TrainNameProperty); }
set { SetValue(TrainNameProperty, value); }
}

private ElementAdorner _elementAdorner = default!;

public TrainLeft()
{
InitializeComponent();

DataContext = this;

MouseMove += DragControl_MouseMove;
PreviewGiveFeedback += DragControl_PreviewGiveFeedback;
}

private void DragControl_PreviewGiveFeedback(object sender, GiveFeedbackEventArgs e)
{
_elementAdorner.SetArrange(this);
}

private void DragControl_MouseMove(object sender, MouseEventArgs e)
{
AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this);
_elementAdorner = new ElementAdorner(this);
adornerLayer.Add(_elementAdorner);

DragDrop.DoDragDrop(this, this.DataContext, DragDropEffects.Copy);

adornerLayer.Remove(_elementAdorner);
}

private static void ShowFirePropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
TrainLeft trainLeft = (TrainLeft)d;
bool show = (bool)e.NewValue;

if (show)
{
ImageBehavior.SetAnimatedSource(trainLeft.imFire,
(ImageSource)Application.Current.FindResource("FireGif"));
ImageBehavior.SetRepeatBehavior(trainLeft.imFire, RepeatBehavior.Forever);
}
else
{
ImageBehavior.SetAnimatedSource(trainLeft.imFire, null);
}
}

private static void ShowDoorPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
TrainLeft trainLeft = (TrainLeft)d;
bool show = (bool)e.NewValue;

if (show)
{
ImageBehavior.SetAnimatedSource(trainLeft.imDoor,
(ImageSource)Application.Current.FindResource("DoorGif"));
ImageBehavior.SetRepeatBehavior(trainLeft.imDoor, RepeatBehavior.Forever);
}
else
{
ImageBehavior.SetAnimatedSource(trainLeft.imDoor, null);
}
}
}


Подробнее здесь: https://stackoverflow.com/questions/781 ... ure-in-wpf
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»