2017-10-19 11 views
1

Je suis encore nouveau à WPF et Binding donc s'il vous plaît être aussi précis que possible. J'essaye de construire une liste d'objets à un ListBox de checkBoxes que je voudrais lier avec un Combobox. Lorsque la zone de liste déroulante est sélectionnée, je voudrais que le contrôle ListBox de checkBox soit actualisé. Le ListBox se charge parfaitement sur le premier chargement mais ne s'actualise pas lorsque la liste d'objets change. J'ai débogué, et je peux voir que les objets changent juste l'interface utilisateur n'est pas déclenchée. Toute aide serait bien, merci d'avance.WPF liste de contrôle avec ComboBox Sélectionnez

ComboBox

<ComboBox Grid.Column="0" SelectionChanged="JobTypeComboBox_SelectionChanged" 
           Name="JobTypeComboBox" 
           ItemsSource="{Binding Path=AllJobTypes}" 
           DisplayMemberPath="Name" 
           SelectedValuePath="Name" 
           SelectedValue="{Binding Path=JobConfig.SelectedJobType.Name}" /> 

cases ListBox

<ListBox ItemsSource="{Binding AllDocTypes}" Height="177" Name="listTopics" VerticalAlignment="Top"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}" Checked="DocTypeCheckBox_Checked" Unchecked="DocTypeCheckBox_UnChecked"/> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

Constructor

public ConfigControl() { 
    InitializeComponent(); 
    this.DataContext = this; 
    LoadSettings(); 
} 

Attributs

// Create the OnPropertyChanged method to raise the event 
protected void OnPropertyChanged(string name) { 
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); 
} 

public JobConfiguration JobConfig { 
    get { return _jobConfig; } 
    set { 
     _jobConfig = value; 
     // Call OnPropertyChanged whenever the property is updated 
     OnPropertyChanged("JobConfig"); 
    } 
} 

public DocTypeList AllDocTypes { 
    get { return _allDocTypes; } 
    set { 
     _allDocTypes = value; 
     OnPropertyChanged("AllDocTypes"); 
    } 
} 

ComboBox Sélectionnez Modifier

private void JobTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { 
    //set the new jobtype selected 
    //load settings for that job type 
    ComboBox cmb = sender as ComboBox; 
    JobType selectedJob = (JobType)cmb.SelectedItem; 
    JobConfig.SelectedJobType = selectedJob; 
    AllDocTypes.SetDocTypeIsChecked(JobConfig.SelectedJobType.DocTypes); 
    OnPropertyChanged("JobConfig"); 
    OnPropertyChanged("AllDocTypes"); 
} 

DocType Classes

using System.Collections.Generic; 
using System.Linq; 
using System.Xml.Serialization; 

namespace ISO_Validation_And_Processing.Models { 

public class DocType { 
    [XmlElement] 
    public string Name { get; set; } 

    [XmlIgnore] 
    public bool IsChecked { get; set; } = false; 
} 

public class DocTypeList : List<DocType> { 
    public static DocTypeList Read(ISerializeManager serializeManager) { 
     if (serializeManager != null) { 
      return serializeManager.ReadObject<DocTypeList>(); 
     } else { 
      return null; 
     } 
    } 

    public DocTypeList() { } 

    public void SetDocTypeIsChecked(DocTypeList selectedDocs) { 
     foreach (var docType in this) { 
      docType.IsChecked = IsDocTypeSelected(docType, selectedDocs); 
     } 
    } 

    public bool IsDocTypeSelected(DocType docType, DocTypeList selectedDocs) { 
     //if there is a doctype with the same name return true 
     return selectedDocs.Where(t => t.Name == docType.Name).ToList().Count > 0; 
    } 
} 
} 
+0

Comment et où AllDocTypes est-il "actualisé"? – mm8

+0

Est-ce que 'AllDocTypes' est une ObservableCollection <>? – MisterMystery

+0

Désolé ajouté le constructeur –

Répondre

2

La classe DocType doit implémenter l'interface INotifyPropertyChanged et augmenter les notifications de modification lorsque la propriété IsChecked est définie:

public class DocType : INotifyPropertyChanged 
{ 
    [XmlElement] 
    public string Name { get; set; } 

    private bool _isChecked; 
    [XmlElement] 
    public bool IsChecked 
    { 
     get { return _isChecked; } 
     set 
     { 
      _isChecked = value; 
      OnPropertyChanged("IsChecked"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

Puis le CheckBox devrait être mis à jour chaque fois que vous appelez le SetDocTypeIsChecked méthode.

+0

Pourriez-vous refactoriser cette réponse pour utiliser mes 2 classes afin que je puisse mieux comprendre pour mettre en œuvre? –

+1

Vous devez ré-implémenter votre classe DocType selon ma réponse éditée. – mm8

+1

Je souhaite que je pourrais upvote ceci plus d'une fois! –