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 89 90 91 92 93 94 95 96 97 98 99
|
namespace Binding.ListBox
{
//GUI
public partial class MainWindow : Window
{
public Group TheGroup { get; set; }
public struct StructContext
{
public List<User> ListUserFull { get; set; }
}
public MainWindow()
{
InitializeComponent();
var context = new StructContext() { ListUserFull = new Business().UserGetAll() };
TheGroup = new Business().GetOneGroup();
TheGroup.Users = TheGroup.Users.Select(oneUser => context.ListUserFull.FirstOrDefault(u => u.Id == oneUser.Id)).ToList();
DataContext = context;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if(TheGroup!=null && TheGroup.Users!=null)
Console.WriteLine("Il y a "+TheGroup.Users.Count+" utilisateurs selectionnés");
else
Console.WriteLine("Il n'y a aucun utilisateur selectionné");
}
}
//BLL
public class Business
{
public List<User> UserGetAll()
{
return new List<User>{
new User(1, "DUPOND", "Martin"),
new User(2, "DURAND", "François"),
new User(3, "MACHIN", "Marc"),
new User(4, "GILLES", "Franck")
};
}
public Group GetOneGroup()
{
return new Group{
Users = new List<User>{
new User(1, "DUPOND", "Martin"),
new User(2, "DURAND", "François")
}
};
}
}
//ENTITY
public class Group
{
public List<User> Users { get; set; }
}
public class User
{
public User(long id, string lastname, string firstname)
{
Id = id;
Lastname = lastname;
Firstname = firstname;
}
public long Id { get; set; }
public string Lastname { get; set; }
public string Firstname { get; set; }
}
//BEHAVIOR
public static class ListBoxSelectedItems
{
public static void SetSelectedItems(System.Windows.Controls.ListBox listbox, IList list) { listbox.SetValue(SelectedItemsProperty, list); }
public static IList GetSelectedItems(System.Windows.Controls.ListBox listbox) { return listbox.GetValue(SelectedItemsProperty) as IList; }
public static DependencyProperty SelectedItemsProperty = DependencyProperty.RegisterAttached("SelectedItems", typeof(IList), typeof(ListBoxSelectedItems), new PropertyMetadata(null, new PropertyChangedCallback(SelectedItemsProperty_Changed)));
private static void SelectedItemsProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (!(d is System.Windows.Controls.ListBox) || !(e.NewValue is IList)) return;
((System.Windows.Controls.ListBox)d).SelectedItems.Clear();
foreach (var oneValue in ((IList)e.NewValue))
((System.Windows.Controls.ListBox)d).SelectedItems.Add(oneValue);
((System.Windows.Controls.ListBox)d).SelectionChanged += ListBox_SelectionChanged;
}
private static void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!(sender is System.Windows.Controls.ListBox)) return;
}
}
} |
Partager