2010-11-26 7 views
-1

나는 ListBox를 가지고 있습니다. 모든 ListBoxItem에는 두 개의 TextBlock과 하나의 CheckBox가 있습니다. 처음에는 모든 확인란을 선택했습니다. 하나의 체크 박스를 선택 해제 한 다음 스크롤하면 체크 박스가 무작위로 선택 및 선택 해제됩니다.목록 상자 항목의 체크 박스가 무작위로 선택/선택 해제되었습니다.

무엇이 잘못 되었나요?

코드 :

cut from [X.xaml.cs]  
    //       
    lbx.ItemsSource = settings.itemSettings; // settings is instance of Settings 

    // holds data for the listbox 
    public class Settings 
    { 
     public ObservableCollection<ItemSetting> itemSettings; // <-lbx.ItemSource 

     public Settings(ObservableCollection<string> texts) 
     { 
      itemSettings = new ObservableCollection<ItemSetting>();    
      for (int i = 0; i < texts.Count; i++) 
      {     
       ItemSetting s = new ItemSetting(); 
       s.number = (i + 1).ToString();  // the position number 
       s.text = texts[i];     // the message 
       s.show = true;      // true=show or false=hide 
       itemSettings.Add(s); 
      } 
     }         
    } 
    // holds data for listbox item 
    public class ItemSetting 
    { 
     public string number { get; set; } 
     public string text { get; set; } 
     public bool show { get; set; }   
    } 

    ------------------------------------------------------------------------------ 
    cut from [X.xaml] --- the listbox 
    <ListBox Name="lbx"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal">             
         <TextBlock Text="{Binding number}" /> 
         <TextBlock Text="{Binding text}" /> 
         <CheckBox IsChecked="{Binding show}" />                        
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

답변

1

솔루션 :

Binding mode TwoWay 
    <CheckBox IsChecked="{Binding show, Mode=TwoWay}" /> 


    Implement INotifyPropertyChanged 
    public class ItemSetting : System.ComponentModel.INotifyPropertyChanged 
    { 
     public string questionNumber { get; set; } 
     public string questionText { get; set; } 
     //public bool show { get; set; } 
     private bool bShow; 

     public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; 
     void Notify(string propName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propName)); 
      } 
     } 

     public bool show 
     { 
      get {return this.bShow;}    
      set {if (value != this.bShow) { this.bShow = value; Notify("show"); }}    
     }    
    } 
1

TwoWay

<CheckBox IsChecked="{Binding show, Mode=TwoWay}" /> 
로 바인딩 모드를 확인
관련 문제