【问题标题】:Checkboxes in listboxitem checked/unchecked randomlylistboxitem 中的复选框随机选中/取消选中
【发布时间】:2010-11-26 16:29:56
【问题描述】:

我有一个列表框。在每个 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>

【问题讨论】:

    标签: checkbox listbox silverlight-4.0


    【解决方案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"); }}             
            }            
        }
    

    【讨论】:

      【解决方案2】:

      将绑定模式设为TwoWay

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

      【讨论】:

        猜你喜欢
        • 2012-06-29
        • 2013-11-04
        • 2019-01-28
        • 2014-11-03
        • 2016-03-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多