public DeuxiemeFenetre() { InitializeComponent(); ListPersonn = new ObservableCollection<Personn> { new Personn { Name = "Jean", Age = 21, Sexe = "H"}, new Personn { Name = "Amélie", Age = 21, Sexe = "F"}, new Personn { Name = "Bernard", Age = 50, Sexe = "H"}, new Personn { Name = "Odette", Age = 50, Sexe = "F"}, new Personn { Name = "Paul", Age = 42, Sexe = "H"}, new Personn { Name = "Delphine", Age = 42, Sexe = "F"}, new Personn { Name = "Thomas", Age = 14, Sexe = "H"}, new Personn { Name = "Noah", Age = 14, Sexe = "F"}, }; DataContext = this; } public static ObservableCollection<Personn> ListPersonn { get; set; } private void Button_Click(object sender, RoutedEventArgs e) { UserControl1 userControl1 = new UserControl1 { Margin = new Thickness(10, 40, 0, 0), VerticalAlignment = VerticalAlignment.Top, HorizontalAlignment = HorizontalAlignment.Left, Height = 100, Width = 280 }; grid.Children.Add(userControl1); } }
<Window x:Class="Test_WPF.DeuxiemeFenetre" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Test_WPF" mc:Ignorable="d" Title="DeuxiemeFenetre" Height="300" Width="600" DataContext="{Binding}"> <Grid Name="grid" DataContext="{Binding .}"> <DataGrid x:Name="PersonnDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ScrollViewer.VerticalScrollBarVisibility="Visible" ItemsSource="{Binding ListPersonn}" DataContext="{Binding .}" Margin="321,10,70,10" RowDetailsVisibilityMode="VisibleWhenSelected" CanUserAddRows="False" > <DataGrid.Columns> <DataGridTextColumn x:Name="NameColumn" Binding="{Binding Name, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="Name"/> <DataGridTextColumn x:Name="AgeColumn" Binding="{Binding Age}" Header="Age" /> <DataGridTextColumn x:Name="SexeColumn" Binding="{Binding Sexe}" Header="Sexe" /> </DataGrid.Columns> </DataGrid> <Button Content="Ajoute Usercontrol" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="118" Click="Button_Click"/> </Grid> </Window>
public partial class UserControl1 : UserControl, INotifyPropertyChanged { public UserControl1() { InitializeComponent(); DataContext = this; DeuxiemeFenetre.ListPersonn.CollectionChanged += ListPersonn_CollectionChanged; } private void ListPersonn_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { switch(e.Action) { case NotifyCollectionChangedAction.Add: case NotifyCollectionChangedAction.Remove: case NotifyCollectionChangedAction.Replace: case NotifyCollectionChangedAction.Reset: GenerePropertyChnaged("Ages");//si on modifie le nombres d'enregistrements break; } } List<Personn> filtres; private void appliqueFiltre() { filtres = DeuxiemeFenetre.ListPersonn.Where(x => x.Age == Convert.ToInt32(AgeCbx.SelectedItem)).ToList(); GenerePropertyChnaged("Filtres"); } public event PropertyChangedEventHandler PropertyChanged; private void AgeCbx_SelectionChanged(object sender, SelectionChangedEventArgs e) { appliqueFiltre(); } public List<Personn> Filtres { get { return filtres; } } public List<int> Ages { get { return DeuxiemeFenetre.ListPersonn.Select(p => p.Age).Distinct().ToList(); } } private void GenerePropertyChnaged(params string[] Props) { foreach(string p in Props) PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(p)); } }
<UserControl x:Class="Test_WPF.UserControl1" 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" xmlns:local="clr-namespace:Test_WPF" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="73*"/> <RowDefinition Height="237*"/> <RowDefinition Height="140*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="120*"/> <ColumnDefinition Width="104*"/> <ColumnDefinition Width="177*"/> <ColumnDefinition Width="187*"/> <ColumnDefinition Width="212*"/> </Grid.ColumnDefinitions> <ComboBox Name="AgeCbx" ItemsSource="{Binding Ages}" SelectionChanged="AgeCbx_SelectionChanged"/> <TextBox x:Name="PrenomtxtA1" Foreground="Red" Grid.Column="2" Grid.Row="2" Text="{Binding Filtres[0].Name}" /> <TextBox x:Name="PrenomtxtA2" Foreground="Red" Grid.Column="3" Grid.Row="2" Text="{Binding Filtres[1].Name}"/> <TextBox x:Name="PrenomtxtA3" Foreground="Red" Grid.Column="4" Grid.Row="2" Text="{Binding Filtres[2].Name}" /> </Grid> </UserControl>
if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Nom_Propriété")); }
GenerePropertyChanged(string Propriete) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(Propriete)); } }
GenerePropertyChanged("Nom_Propriété");
mesPrenoms[i].Text = Prenoms[i].ToString();
<TextBox x:Name="PrenomtxtA2" Style="{StaticResource Textbox5RoundWhiteStyle}" Foreground="Red" Grid.Column="3" Grid.Row="2" Text="{Binding Name}"/>
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionIl y a juste un petit truc que je ne capte pas c'est le datacontext= {Binding .}.
Est ce que ça veut dire que le controle hérite du datacontext de son Parent?