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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
//needed
using WpfIndexGridViewCS.Properties;
using System.Collections.Specialized;
using System.Reflection;
using System.Configuration;
namespace WpfIndexGridViewCS
{
/// <summary>
/// Logique d'interaction pour Window1.xaml
/// </summary>
public partial class WinIndexGridView : Window
{
//classe et collection exemple
private Emplacement objEmplacement ;
private Emplacements objEmplacements = new Emplacements();
//variable
private GridViewColumn gvc ;
// classe ColSettings ad-hoc (parametres Setting User) avec 2 props :
// *rop EtatInitial =True
// *prop lstCols (liste des colonnes à memoriser)
// classe MapCol:mappe 2 colonnes à memoriser :Header et Displaymember.Path
private ColSettings myPrefsBis=new ColSettings();
public WinIndexGridView()
{
InitializeComponent();
this.DataContext = AddPlaces();
// pour resetter lors d'un test il suffit de
// remettre myPrefs.EtatInitial à true une fois seulement
// ensuite "comment it" dans les appels suivants
// pour voir comment la position de chaque header est memorise
// myPrefsBis.EtatInitial = true;
}
private Emplacements AddPlaces()
{
objEmplacement = new Emplacement();
objEmplacement.CityName = "Paris";
objEmplacement.State = "France";
objEmplacement.Population = 1400000;
objEmplacement.SpecialFeatures = SpecialFeatures.None;
objEmplacements.Add(objEmplacement);
objEmplacement = new Emplacement();
objEmplacement.CityName = "Lyon";
objEmplacement.State = "France";
objEmplacement.Population = 5000000;
objEmplacement.SpecialFeatures = SpecialFeatures.Highlight;
objEmplacements.Add(objEmplacement);
objEmplacement = new Emplacement();
objEmplacement.CityName = "Dijon";
objEmplacement.State = "France";
objEmplacement.Population = 500000;
objEmplacement.SpecialFeatures = SpecialFeatures.Color;
objEmplacements.Add(objEmplacement);
return objEmplacements;
}
private void btnAddPlaces_Click(object sender, RoutedEventArgs e)
{
objEmplacement = new Emplacement();
objEmplacement.CityName = "Marseille";
objEmplacement.State = "France";
objEmplacement.Population = 600000;
objEmplacement.SpecialFeatures = SpecialFeatures.None;
objEmplacements.Add(objEmplacement);
objEmplacement = new Emplacement();
objEmplacement.CityName = "Toulouse";
objEmplacement.State = "France";
objEmplacement.Population = 6000000;
objEmplacement.SpecialFeatures = SpecialFeatures.Highlight;
objEmplacements.Add(objEmplacement);
objEmplacement = new Emplacement();
objEmplacement.CityName = "Nantes";
objEmplacement.State = "France";
objEmplacement.Population = 300000;
objEmplacement.SpecialFeatures = SpecialFeatures.Color;
objEmplacements.Add(objEmplacement);
}
// Load userSettings
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("etat initial :" + myPrefsBis.EtatInitial);
if (myPrefsBis.EtatInitial)
{
InitialisePrefs();
}
//Affecte liste headers et nom props sauves à myGridView
this.myGridView.Columns.Clear();
foreach (MapCol mappe in myPrefsBis.lstCols)
{
gvc = new GridViewColumn();
gvc.DisplayMemberBinding = new Binding(mappe.nameProp) ;
gvc.Header = mappe.Header;
gvc.Width = 100;
this.myGridView.Columns.Add(gvc);
}
//Bind ListviewTest.view to myGridViewTest
this.myListeView.View = myGridView;
}
// Save userSettings
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//Sauve liste headers et noms props à partir de myGridView
if (myPrefsBis == null)
{
return;
}
this.myPrefsBis.lstCols.Clear();
MapCol mappe ;
foreach (GridViewColumn col in myGridView.Columns)
{
mappe=new MapCol() ;
//nom headers
mappe.Header = col.Header.ToString();
//nom props
Binding bd = (Binding)col.DisplayMemberBinding;
mappe.nameProp = bd.Path.Path;
myPrefsBis.lstCols.Add(mappe);
}
//sauvegarde
myPrefsBis.Save();
}
// Initialiser les userSettings
// à partir de ordre par defaut des colonnes du GridView savoir :
// l'ordre de declaration des props dans classe
private void InitialisePrefs()
{
myPrefsBis.lstCols = new List<MapCol>();
MapCol mappe;
foreach (GridViewColumn col in this.myGridView.Columns)
{
mappe = new MapCol();
//nom headers
mappe.Header=col.Header .ToString();
//nom props(caster BindingBase sur Binding et retrouver Binding.Path.Path)
Binding bd = (Binding)col.DisplayMemberBinding;
mappe.nameProp =bd.Path.Path;
myPrefsBis.lstCols.Add(mappe);
}
//Sauvegarde etat initial
//on n'est plus au 1er lancement d'application
myPrefsBis.EtatInitial = false;
myPrefsBis.Save();
}
}
// Application settings wrapper class.
// This class defines the settings we intend to use in our application.
class ColSettings : ApplicationSettingsBase
{
//default value de EtatInitial est mise
//à true pour premier lancement app. ce qui
//pour effet d'appeler procedure InitialisePrefs().
[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute("True")]
public bool EtatInitial
{
get { return (bool)(this["EtatInitial"]); }
set { this["EtatInitial"] = value; }
}
[UserScopedSetting()]
[DefaultSettingValueAttribute("")]
public List<MapCol> lstCols
{
get { return (List<MapCol>)(this["lstCols"]); }
set { this["lstCols"] = value; }
}
}
// classe qui mappe le Header et le DisplayMember.Path
public class MapCol
{
public string Header { get; set; }
public string nameProp { get; set; }
public MapCol()
{ }
}
} |
Partager