2010-11-26 13 views
-1

私はリストボックスを持っています。すべてのListBoxItemには、2つのTextBlockと1つのCheckBoxがあります。最初はすべてのチェックボックスをチェックしました。 1つのチェックボックスをオフにしてからスクロールすると、チェックボックスはランダムにチェックされ、チェックされなくなります。リストボックス項目のチェックボックスがランダムにチェックされているかチェックされていない

どういうところが間違っていますか?

コード:

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}" /> 
として結合様式を作ります
関連する問題