Hello,
I am trying to bind to datagrid several data. I created an observablecollection, i stocked data into this collection and then i binded this collection to my datagrid. In xaml file i declared my datacontext. When i did it my visual studio and all application on my computer clock. I can't do anything. A simple think that i can do, is to close session and restart .
I need help.
Here is some sample:
code cs ViewModel:
using System;
using System.Collections.Generic;
using System.Text;
using GestionDeContrats_Offres_ClientsGUI.VueModele;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.ComponentModel;
using GestionDeContrats_Offres_Clients.GestionOffres;
using GestionDeContrats_Offres_Clients.GestionContrats;
using System.Windows.Input;
using GestionDeContrats_Offres_Clients.GestionModele;
using GestionDeContrats_Offres_ClientsGUI.crm;
using System.Data;
namespace GestionDeContrats_Offres_ClientsGUI.VueModele
{
/// <summary>
///
/// </summary>
public class GestionDeContratVueModele : VueModeleBase
{
private readonly ObservableCollection<ContratVueModele> contrats;
private readonly PagingCollectionView pagingView;
private GestionDeContrat gestiondecontrat;
/// <summary>
/// Constructeur de la classe
/// GestionDeContratVueModele
/// </summary>
public GestionDeContratVueModele() {
try
{
this.gestiondecontrat = new GestionDeContrat();
this.contrats = new ObservableCollection<ContratVueModele>();
this.contrats.Clear();
foreach (contract contrat in this.gestiondecontrat.ListeDeContrat())
{
// this.contrats.Add(new ContratVueModele());
this.contrats.Add(new ContratVueModele() { NOMDUCONTRAT = contrat.title, DATEDEDEBUT = contrat.activeon.Value, DATEDEFIN
= contrat.expireson.Value, LESTATUT = contrat.statecode.formattedvalue,LESTATUTAVANT=contrat.access_etatavant.name });
}
this.pagingView = new PagingCollectionView(this.contrats, 3);
if (this.pagingView == null)
throw new NullReferenceException("pagingView");
this.currentpage = this.pagingView.CurrentPage;
this.pagingView.CurrentChanged += new EventHandler(pagingView_CurrentChanged);
}
catch(System.Web.Services.Protocols.SoapException soapEx){
soapEx.Detail.OuterXml.ToString();
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void pagingView_CurrentChanged(object sender, EventArgs e)
{
OnPropertyChanged("SelectedContrat");
Dispose();
//throw new NotImplementedException();
}
/// <summary>
/// Propriété permettant de manipuler la
///Vue Modèle de la liste des contrats
/// </summary>
public ObservableCollection<ContratVueModele> Lescontrats
{
get
{
return this.contrats;
}
}
}
}
code xaml:
<UserControl x:Class="GestionDeContrats_Offres_ClientsGUI.VueModele.UserControlGestionContrat"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
x:Name="GestionContrat"
xmlns:local="clr-namespace:GestionDeContrats_Offres_ClientsGUI.VueModele"
d:DesignHeight="300" >
<UserControl.DataContext>
<local:GestionDeContratVueModele />
</UserControl.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="40"/>
<RowDefinition />
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="320"/>
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<TextBox Name="searchtexbox" Grid.Column="0"/>
<Image Grid.Column="1" Source="/GestionDeContrats_Offres_ClientsGUI;component/Images/16_find.gif" />
</Grid>
<ToolBar Grid.Row="1" Name="toolbarcontrat">
<Button Name="btNewContrat" Click="btNewContrat_Click">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Source="/GestionDeContrats_Offres_ClientsGUI;component/Images/plusvert.jpg" />
<Label Content="Nouveau" Grid.Column="1"/>
</Grid>
</Button>
<Button Name="btCopierContrat" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Source="/GestionDeContrats_Offres_ClientsGUI;component/Images/editcopy.png" />
<Label Content="Copier" Grid.Column="1"/>
</Grid>
</Button>
<Button Name="btSupprimerContrat" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Source="/GestionDeContrats_Offres_ClientsGUI;component/Images/delgreen16.jpg" />
<Label Content="Supprimer" Grid.Column="1"/>
</Grid>
</Button>
<Button Name="btModifierContrat" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Source="/GestionDeContrats_Offres_ClientsGUI;component/Images/ico_18_4207.gif" />
<Label Content="Modifier" Grid.Column="1"/>
</Grid>
</Button>
</ToolBar>
<DataGrid Name="listViewContrat" Grid.Row="2" ItemsSource="{Binding Path=Lescontrats, Mode=OneWay}" IsSynchronizedWithCurrentItem="True" AutoGenerateColumns="False"
CanUserReorderColumns="True" CanUserResizeColumns="True" CanUserSortColumns="True" CanUserAddRows="True" CanUserDeleteRows="True">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Nom du contrat" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=NOMDUCONTRAT, Mode=OneWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Date de début" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=DATEDEDEBUT, Mode=OneWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Date de fin" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=DATEDEFIN, Mode=OneWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Statut" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=LESTATUT,Mode=OneWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Statut avant" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text=""/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<StackPanel Grid.Row="3" Orientation="Horizontal">
<Label Margin="2" Content=""/>
<Button Content="Suivant" Name="btNext" Margin="2" />
<Button Content="Précédent" Name="btPrevious" Margin="2" />
</StackPanel>
</Grid>
</UserControl>
I include link to this usercontrol into MainWindow.xaml.
Thanks
Regards