Gérer combobox différente d'un grid

yann458 Messages postés 547 Statut Membre -  
yann458 Messages postés 547 Statut Membre -
Bonjour,
J'ai un projet WPF d'un grid avec combobox à items différente, quand je clique sur un combobox , tous s'efface dans la colonne du combobox.

Je vous joint le projet qui fonctionne mais mal.

Merci beaucoup.

https://www.filehosting.org/

6 réponses

  1. Utilisateur anonyme
     
    Bonjour

    et si tu postais plutôt le code du xaml et la partie de code behind qui gère la combobox dans le fil?

    Voir ici comment poster un code lisible.
    0
  2. yann458 Messages postés 547 Statut Membre
     
    Ca ne coute rien de compiler un projet.

    MainWindow.xaml
    <Window x:Class="WpfApplicationGrid.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplicationGrid"  
            Title="MainWindow" Height="350" Width="525">
        <Window.DataContext>
            <local:viewmodel/>
        </Window.DataContext>
        <Window.Resources>
            <!-- CollectionViewSourceで参照出来るようにしておいて -->
            <CollectionViewSource   x:Key="ListSource"   Source="{Binding Path=Employees.lalist}" />
            <CollectionViewSource   x:Key="ResultatSource"   Source="{Binding resultat}" />
    
            <!--<DataTemplate x:Key="EmpTemplate" DataType="{x:Type local:Employee}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding resultat}"></TextBlock>                
                </StackPanel>
            </DataTemplate>-->
        </Window.Resources>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="32"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0">
                <DataGrid AutoGenerateColumns="False" Name="myGrid" Margin="10" ItemsSource="{Binding Employees}" SelectionChanged="myGrid_SelectionChanged">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Name"  Binding="{Binding Path=Name}" />
                        <DataGridComboBoxColumn x:Name="truccombo" Header = "Party" SelectedItemBinding = "{Binding Path=resultat}"                   
                                                DisplayMemberPath="Name" 
                                                />
                        <DataGridTextColumn Header="RName"  Binding="{Binding Assign,Mode=OneWay}"  />
    
    
                    </DataGrid.Columns>
                </DataGrid>
            </Grid>
            <Grid Grid.Row="1">
                <Button Content="Button" HorizontalAlignment="Left" Height="12" Margin="138,10,0,0" VerticalAlignment="Top" Width="91" Click="Button_Click"/>
    
            </Grid>
        </Grid>
    </Window>
    

    mainwindow.xaml.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    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;
    
    namespace WpfApplicationGrid
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void myGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (e.AddedItems.Count > 0)
                {
                    object o = e.AddedItems[0];
                    Employee ee = o as Employee;
                    if (ee != null)
                    {
                        truccombo.ItemsSource = ee.lalist;
                    }
                }
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                int i = 4;
                e = e;
            }
        }
    }
    
    

    viewmodel.cs
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace WpfApplicationGrid
    {
        public class Lttt
        {
            private string _name;
            public string Name
            {
                get
                {
                    return _name;
                }
                set
                {
                    _name = value;
                }
    
            }
            public Lttt(string s)
            {
                _name = s;
            }
        }
        public class Employee : INotifyPropertyChanged
        {
    
            public event PropertyChangedEventHandler PropertyChanged;
            ObservableCollection<Lttt> _lista;
           /* public static ObservableCollection<string> _Genders;
            public ObservableCollection<string> Genders
            {
                get
                {
                    return _Genders;
                }
                set
                {
                    _Genders = value;
                }
            }*/
            private Lttt _resultat;
            public Lttt resultat
            {
                get
                {
                    return _resultat;
                }
                set
                {
                    _resultat = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("Assign"));
                    }
                }
            }
            public string Name { get; set; }
            public string Assign {
                get
                {
                    string sd = "";
                    if (_resultat!=null)
                        sd = _resultat.Name;
                    return sd;
                }
           }
            public ObservableCollection<Lttt> lalist
            {
                get
                {
                    return _lista;
                }
    
            }
    
            public Employee(string _sn, ObservableCollection<Lttt> maliste)
            {
                Name = _sn;
                //Gender = _sg;
                _resultat = null;
                _lista = maliste;
    
    
            }
        }
        public class viewmodel
        {
            //private Lttt _resultat;
            public static ObservableCollection<Employee> Employees { get; set; }
    
    
            public viewmodel()
            {
    
    
                ObservableCollection<Lttt> _lista,_listb;
    
                _lista = new ObservableCollection<Lttt>();
                _lista.Add(new Lttt("1"));
                _lista.Add(new Lttt("2"));
                _lista.Add(new Lttt("3"));
                _listb = new ObservableCollection<Lttt>();
                _listb.Add(new Lttt("4"));
                _listb.Add(new Lttt("5"));
                _listb.Add(new Lttt("6"));
                Employees = new ObservableCollection<Employee>()
                {
                    new Employee("ABCgroup1",_lista),
                    new Employee("CDEFgroup1",_lista),
                    new Employee("UVDgroup2",_listb),
                    new Employee("XYZgroup2",_listb),
                };
    
                //myGrid.ItemsSource = Employees;
                //Gender.ItemsSource = Genders;
            }        
        }
    }
    


    EDIT : Ajout des balises de code (la coloration syntaxique).
    Explications disponibles ici : ICI

    Merci d'y penser dans tes prochains messages.
    0
    1. Utilisateur anonyme
       
      Tout le monde n'est pas chaud à télécharger un zip provenant d'un membre qu'on ne connait pas plus que ça.
      De plus n'ayant pas Visual Studio sous la main, si ça avait été simple j'aurais peut-être pu t'aider.
      0
    2. Utilisateur anonyme
       
      Merci à Baladur d'avoir corrigé ta coloration syntaxique, c'ets plus lisible.

      J'ai voulu télécharger ton projet, mais il faut rentrer son adresse mail.
      Il y a des services gratuits ou cela n'est pas nécessaire.
      cijoint.com, dropbox, free gros fichiers etc...
      0
  3. Utilisateur anonyme
     
    Donc en me servant des codes postés dans la fil, j'ai pu tester.



    Là en choisissant 5 dans la combox box de la ligne VUVDgroup2, cela a effacer le 2 de la colonne Party de CDEgroup1. Et si je comprends bien tu voudrait qu'il reste affiché?
    0
  4. yann458 Messages postés 547 Statut Membre
     
    oui qu'ils restent afficher.
    0
  5. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  6. yann458 Messages postés 547 Statut Membre
     
    merci
    0