Bonjour,
Je cherche un moyen de gérer des évènements sur un élément d’une ListBox construite par Binding sur une ObservableCollection<>. Je programme sur Windows Phone 8.0 en C#.
J’aimerais que ma ListBox réagisse comme une liste sur iOS : que les lignes réagissent à un swipe (« swipe to delete ») et pouvoir les réordonner.
Ce que j’aimerais donc c’est savoir comment faire réagir cet élément à un mouvement horizontal (soit pour faire glisser l'item, soit pour faire glisser un StackPanel contenu dans cet item), comment animer cet élément (pour par exemple le faire revenir à sa place quand je commence à déplacer un autre élément de la liste). Sur iOS cela correspondrait à une SWTableViewCell, présentée ici : https://github.com/CEWendel/SWTableViewCell
Dans un second temps j’aimerai aussi pouvoir réorganiser ma ListBox en faisant glisser verticalement les éléments pour les mettre dans l’ordre souhaité.
Afin de faire cela j’ai fait un petit code d’exemple qui ne sert à rien mais qui permet de partir de quelque chose.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 <phone:PhoneApplicationPage x:Class="PhoneApp1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock Text="APP NAME" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/> <TextBlock Text="Page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> </StackPanel> <Grid x:Name="ContentPanel" Grid.Row="1"> <StackPanel> <ListBox x:Name="MainPageList" ItemsSource="{Binding ListBehind}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal" Background="#7F7F7F7F" Width="480"> <Rectangle Height="40" Width="40" Fill="{Binding RectangleColor}" Stroke="Transparent" StrokeThickness="11"/> <TextBlock Text="{Binding Text}" VerticalAlignment="Center"/> </StackPanel> <Button Content="Clic-me !"/> <!-- Le bouton caché que je lierai par la suite à un évènement --> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </StackPanel> </Grid> </Grid> </phone:PhoneApplicationPage>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 using System; using System.Collections.Generic; using System.Net; using System.Windows; using Microsoft.Phone.Controls; using PhoneApp1.Resources; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Collections.ObjectModel; using System.Windows.Media; namespace PhoneApp1 { public class Element : INotifyPropertyChanged { private string _RectangleColor; private string _Text; public string RectangleColor { get { return _RectangleColor; } set { NotifyPropertyChanged(ref _RectangleColor, value); } } public string Text { get { return _Text; } set { NotifyPropertyChanged(ref _Text, value); } } // fonctions pour le Binding qui seront utiles une fois que je saurais faire ce que je cherche à faire public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string PropertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(PropertyName)); } private bool NotifyPropertyChanged<T>(ref T var, T value, [CallerMemberName] string PropertyName = null) { if (object.Equals(var, value)) return false; var = value; NotifyPropertyChanged(PropertyName); return true; } } public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged { private ObservableCollection<Element> _ListBehind; public MainPage() { InitializeComponent(); _ListBehind = new ObservableCollection<Element>(); _ListBehind.Add(new Element { RectangleColor = "#FFFF0000", Text = "Red" }); _ListBehind.Add(new Element { RectangleColor = "#FF00FF00", Text = "Green" }); _ListBehind.Add(new Element { RectangleColor = "#FF0000FF", Text = "Blue" }); _ListBehind.Add(new Element { RectangleColor = "Aqua", Text = "Aqua" }); _ListBehind.Add(new Element { RectangleColor = "Orange", Text = "Orange" }); _ListBehind.Add(new Element { RectangleColor = "Gold", Text = "Gold" }); DataContext = this; } public ObservableCollection<Element> ListBehind { get { return _ListBehind; } set { NotifyPropertyChanged(ref _ListBehind, value); } } public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string PropertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(PropertyName)); } private bool NotifyPropertyChanged<T>(ref T var, T value, [CallerMemberName] string PropertyName = null) { if (object.Equals(var, value)) return false; var = value; NotifyPropertyChanged(PropertyName); return true; } } }
Partager