2017-10-19 66 views
1

我还是新来WPF和绑定,所以请尽可能具体。我正在尝试构建一个ListBox的ListBox对象列表,我想用一个Combobox来绑定。当选择组合框时,我希望刷新CheckBox的列表框。 ListBox在第一次加载时完美加载,但在对象列表更改时不刷新。我已经调试过了,我可以看到对象正在改变它只是UI没有被触发。任何帮助都会很好,提前谢谢你。WPF清单绑定与组合框选择

组合框

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

列表框复选框

<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> 

构造

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

属性

// 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"); 
    } 
} 

组合框选择更改

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类

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

AllDocTypes如何以及在哪里“刷新”? – mm8

+0

“AllDocTypes”是一个ObservableCollection <>吗? – MisterMystery

+0

对不起,添加构造函数 –

回答

2

DocType类应实现INotifyPropertyChanged接口,并当IsChecked属性设置提高更改通知:

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)); 
    } 
} 

然后CheckBox只要你致电,就应该更新方法。

+0

你可以重构这个答案来使用我的2个类,这样我可以更好地理解实现? –

+1

您需要按照我编辑的答案重新实现您的DocType类。 – mm8

+1

我希望我能不止一次地对此赞不绝口! –