Binder dans un template sur l'index de colone
Bonjour,
J'ai une application d'import de données qui passe par un opération de mappage de champ.
Pour cela j'ai une DataGrid qui m'affiche mes données à importer issus de .csv, l'utilisateur peut
dans chaque entête de colonne choisir par un ComboBox dans quel champ iront les données.
Ma datagrid fonctionnel ressemble donc à ca:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <DataGrid HorizontalAlignment="Left" Margin="10,93,0,0" VerticalAlignment="Top" Height="148" Width="606" ItemsSource="{Binding Path=List, Converter={StaticResource ResourceKey=ArrayToDataViewConverter}}" HeadersVisibility="Column" >
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader" >
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}" >
<ComboBox Name="cb" ItemsSource="{Binding Path=DataContext.ListCombo, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}">
<i:Interaction.Triggers >
<i:EventTrigger EventName="SelectionChanged" >
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding Path=DataContext.SelectionChanged, Mode=OneWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid},AncestorLevel=1}}" PassEventArgsToCommand="True" ></GalaSoft_MvvmLight_Command:EventToCommand>
</i:EventTrigger>
<i:EventTrigger EventName="Loaded" >
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding Path=DataContext.LoadedCb, Mode=OneWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid},AncestorLevel=1}}" PassEventArgsToCommand="True" ></GalaSoft_MvvmLight_Command:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid> |
Mon problème c'est que j'ai été obliger de passer par le event Loaded de mon combobox pour binder mon SelectedItem:
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
| private RelayCommand<RoutedEventArgs> _LoadedCb;
public RelayCommand<RoutedEventArgs> LoadedCb
{
get
{
return (_LoadedCb
?? (_LoadedCb = new RelayCommand<RoutedEventArgs>(
a =>
{
ComboBox c = (ComboBox)a.Source;
System.Windows.Controls.Primitives.DataGridColumnHeader c1 = (System.Windows.Controls.Primitives.DataGridColumnHeader)c.TemplatedParent;
if (c1.Column != null)
{
Binding bin = new Binding();
bin.Source = ListColMap[c1.Column.DisplayIndex];
bin.Mode = BindingMode.TwoWay;
bin.Path = new System.Windows.PropertyPath("MapGP", null);
BindingOperations.SetBinding(c, System.Windows.Controls.ComboBox.SelectedItemProperty, bin);
ListColMap[c1.Column.DisplayIndex].MapGP = "Non défini";
}
})));
}
} |
Ce qui je pense dois pouvoir ce faire en xaml non ?
Avec une syntaxe ou je pourrai faire varier mon index de colonne pour binder au bon objet.
Si cela avait été une ligne je crois pouvoir utiliser un AlternationCount(cf:http://www.developpez.net/forums/d10...ition-binding/) mais dans le cas d'une colonne ??
Merci d'avance