2017-10-19 1 views
1

저는 WPF와 Binding에 아직 익숙하지 않으므로 가능한 구체적으로 작성하십시오. 내가 콤보 상자와 바인딩하려는 checkBoxes의 ListBox 개체의 목록을 작성하려고합니다. 콤보 상자를 선택하면 ListBox의 ListBox를 새로 고치고 싶습니다. ListBox는 첫 번째로드에서 완벽하게로드되지만 객체 목록이 변경되면 새로 고치지 않습니다. 디버깅을했는데 UI가 트리거되지 않고 객체가 그것을 변경하고 있음을 알 수 있습니다. 어떤 도움이라도 좋을 것입니다. 미리 감사드립니다.WPF Checklist ComboBox로 바인딩 선택

콤보

<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

나는 이것을 한 번 이상 upvote 할 수 있으면 좋겠다! –