2 pièce(s) jointe(s)
[WPF]DataGrid avec contenu personnalisé et contenu variable
Bonjour à tous,
Je souhaite réaliser une interface qui afficherait le résultat d'analyse sur des liste d'objets.
Voici les classes utilisés lors de l'analyse :
Code:
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
|
/// <summary>
///
/// </summary>
public class ObjectFormatCheck
{
public ObjectFormatCheck()
{
LsAttributs = new List<AttributFormatCheck>();
}
/// <summary>
/// List des attributs analysé de l'objet analysé
/// </summary>
public List<AttributFormatCheck> LsAttributs { get; set; }
/// <summary>
/// Objet sur lequel a été réalisé l'analyse
/// </summary>
public object CheckedObject { get; set; }
}
/// <summary>
///
/// </summary>
public class AttributFormatCheck
{
/// <summary>
/// Nom de l'attribut
/// </summary>
public string Nom { get; set; }
/// <summary>
/// Valeur de l'attribut
/// </summary>
public object Valeur { get; set; }
/// <summary>
/// Vrai si l'analyse a validé l'attribut
/// </summary>
public bool IsCorrect { get; set; }
/// <summary>
/// Message d'erreur
/// </summary>
public string ErrorMessage { get; set; }
/// <summary>
/// Criticité d'une potentiel erreur
/// </summary>
public CriticitéAttribut Criticité { get; set; }
/// <summary>
/// Vrai si l'erreur a été corrigée
/// </summary>
public bool Corrected { get; set; }
}
public enum CriticitéAttribut
{
Indispensable,
Optionnel,
Innutile
} |
Voici l'un des différents type d'objets que je veux analyser :
Code:
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
|
public class Generalite
{
public string COUNTRY { get; set; }
public string COUNTRY_CODE { get; set; }
public string IDE { get; set; }
public string projet_ide { get; set; }
public string ide_GEOM { get; set; }
public string ide_Type { get; set; }
public string SURVEY_NAME_IDA_IDT { get; set; }
public string IDA { get; set; }
public string projet_ida { get; set; }
public string IDA_GEOM { get; set; }
public string IDA_Type { get; set; }
public string SURVEY_NAME_IDA { get; set; }
public string LEASE_MARC { get; set; }
public string SURVEY_TYPE { get; set; }
public DateTime START_DATE { get; set; }
public DateTime END_DATE { get; set; }
public string CONTRACTOR { get; set; }
public string CONTRACTOR_2G { get; set; }
public string CREW { get; set; }
public string Operator { get; set; }
public string IDE_2G { get; set; }
public string IDA_2G { get; set; }
public string IDA_IDE_2G { get; set; }
public string ORDER { get; set; }
} |
Voici une partie de la fonction qui fait l'analyse de l'objet en question :
Code:
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
|
public static List<ObjectFormatCheck> CheckFormatGénéralités(List<Generalite> généralitésToCheck)
{
var lsCheckedGénéralités = new List<ObjectFormatCheck>();
foreach (var généralitéToCheck in généralitésToCheck)
{
var objectChecked = new ObjectFormatCheck { CheckedObject = généralitéToCheck };
var isCorrect = false;
var errorMessage = "";
//COUNTRY
if (généralitéToCheck.COUNTRY == null || généralitéToCheck.COUNTRY == string.Empty)
{
isCorrect = false;
errorMessage = "la valeur n'est pas renseignée";
}
else
{
isCorrect = true;
errorMessage = "";
}
objectChecked.LsAttributs.Add(new AttributFormatCheck
{
Criticité = CriticitéAttribut.Optionnel,
Nom = "COUNTRY",
Valeur = généralitéToCheck.COUNTRY,
IsCorrect = isCorrect,
ErrorMessage = errorMessage
});
//COUNTRY_CODE
if (généralitéToCheck.COUNTRY_CODE == null || généralitéToCheck.COUNTRY_CODE == string.Empty)
{
isCorrect = false;
errorMessage = "la valeur n'est pas renseignée";
}
else if (généralitéToCheck.COUNTRY_CODE.Count() != 2)
{
isCorrect = false;
errorMessage = "le code pays doit être sur deux caractères";
}
else
{
isCorrect = true;
errorMessage = "";
}
objectChecked.LsAttributs.Add(new AttributFormatCheck
{
Criticité = CriticitéAttribut.Indispensable,
Nom = "COUNTRY_CODE",
Valeur = généralitéToCheck.COUNTRY_CODE,
IsCorrect = isCorrect,
ErrorMessage = errorMessage
});
//...
lsCheckedGénéralités.Add(objectChecked);
}
return lsCheckedGénéralités; |
Ce que je cherche à faire suite à l'analyse :
Pièce jointe 155862
Ce que j'ai fais :
Surcharge d'une datagrid pour permettre de faire un style personnalisé sur des colonnes générées automatiquement (source : http://wpftutorial.net/DataGrid.html)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public class CustomTemplateDataGrid : DataGrid
{
public DataTemplateSelector CellTemplateSelector
{
get { return (DataTemplateSelector)GetValue(CellTemplateSelectorProperty); }
set { SetValue(CellTemplateSelectorProperty, value); }
}
public static readonly DependencyProperty CellTemplateSelectorProperty =
DependencyProperty.Register("Selector", typeof(DataTemplateSelector), typeof(CustomTemplateDataGrid),
new FrameworkPropertyMetadata(null));
protected override void OnAutoGeneratingColumn(DataGridAutoGeneratingColumnEventArgs e)
{
e.Cancel = true;
Columns.Add(new DataGridTemplateColumn
{
Header = e.Column.Header,
CellTemplateSelector = CellTemplateSelector
});
}
} |
L'implémentation de mon contrôle personnalisé :
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
<!--...-->
xmlns:lib="clr-namespace:_2gDataRoomViewer.Lib"
<!--...-->
<Window.Resources>
<lib:FormatCheckDataTemplateSelector x:Key="TemplateSelector"/>
</Window.Resources>
<!--...-->
<lib:CustomTemplateDataGrid Grid.Row="1" Grid.ColumnSpan="5" x:Name="_dtg_pbFormats" Margin="10" AutoGenerateColumns="True"
CellTemplateSelector="{StaticResource TemplateSelector}" ItemsSource="{Binding LsFormatAnalyse}"/> |
Le code_behind :
Code:
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
|
/// <summary>
/// Logique d'interaction pour VisualisationProblemesPreloading.xaml
/// </summary>
public partial class VisualisationProblemesPreloading : Window
{
public VisualisationProblemesPreloading(List<ObjectFormatCheck> formatAnalyse)
{
InitializeComponent();
DataContext = this;
formatAnalyse.ForEach(el =>
{
LsFormatAnalyse.Add(el.LsAttributs.ToList());
});
}
private ObservableCollection<List<AttributFormatCheck>> _lsFormatAnalyse = new ObservableCollection<List<AttributFormatCheck>>();
public ObservableCollection<List<AttributFormatCheck>> LsFormatAnalyse
{
get { return _lsFormatAnalyse; }
set { _lsFormatAnalyse = value; }
}
} |
Ma classe DataTemplateSelector :
Code:
1 2 3 4 5 6 7 8 9 10 11
|
class FormatCheckDataTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(
object item,
DependencyObject container)
{
Window wnd = Application.Current.MainWindow;
return wnd.FindResource("FormatCheckTemplate") as DataTemplate;
}
} |
Voici mon dataTemplate :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
<!--...-->
xmlns:conv="clr-namespace:_2gDataRoomViewer.Converter"
<!--...-->
<conv:BoolToReverseVisibility x:Key="BoolToReverseVisibility"/>
<conv:BoolToVisibility x:Key="BoolToVisibility"/>
<conv:FormatCheckToBrush x:Key="FormatCheckToBrush"/>
<!--Template pour les problèmes de formats de préloading-->
<DataTemplate x:Key="FormatCheckTemplate">
<Grid>
<TextBox Visibility="{Binding IsCorrect, Converter={StaticResource BoolToReverseVisibility}}"
Background="{Binding ., Converter={StaticResource FormatCheckToBrush}}"
Text="{Binding Valeur}" ToolTip="{Binding ErrorMessage}"/>
<TextBlock Visibility="{Binding IsCorrect, Converter={StaticResource BoolToVisibility}}"
Text="{Binding Valeur}"/>
</Grid>
</DataTemplate>
<!--...--> |
Et voici ce que j'obtient :
Pièce jointe 155863
Voilà, je ne sais pas si j'ai été clair mais je suis un peu bloqué.
Je pense qu'il faudrait que j'arrive à générer une classe en cours d'exécution pour recevoir chaque ligne analysée (actuellement de type List<AttributFormatCheck>) mais je sais pas trop comment faire ça ni si c'est la bonne solution.
Merci à vous :)