开发者

WPF Check/Uncheck all checkboxes located in a gridview

开发者 https://www.devze.com 2022-12-24 05:38 出处:网络
I have a gridview with some columns. One of these columns is checkbox type. Then I havetwo buttons in my UI, one for check all and another for uncheck all. I would like to check all checkboxes in the

I have a gridview with some columns. One of these columns is checkbox type. Then I have two buttons in my UI, one for check all and another for uncheck all. I would like to check all checkboxes in the column when I press the a button and uncheck all checkboxes when I press the another one. How can I do this?

Some snippet code:

<...>

                    <Classes:SortableListView 
                            x:Name="lstViewRutas"                                      
                            ItemsSource="{Binding Source={StaticResource 
                                          RutasCollectionData}}" ... >
                     <...>
                    <GridViewColumn Header="Activa" Width="50">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox  x:Name="chkBxF"  
                                           Click="chkBx_Click"
                                           IsChecked="{Binding Path=Activa}"    
                                           HorizontalContentAlignment="Stretch" 
          开发者_开发百科                                 HorizontalAlignment="Stretch"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <...>
                    </Classes:SortableListView>

                    <...>
                    </Page>

My data object binding to gridview is:

   namespace GParts.Classes
   {
   public class RutasCollection
   {

    /// <summary>
    /// Colección de datos de la tabla
    /// </summary>
    ObservableCollection<RutasData> _RutasCollection;

    /// <summary>
    /// Constructor. Crea una nueva instancia tipo ObservableCollection
    /// de tipo RutasData
    /// </summary>
    public RutasCollection()
    {               
      _RutasCollection = new ObservableCollection<RutasData>();
    }

    /// <summary>
    /// Retorna el conjunto entero de rutas en la colección
    /// </summary>
    public ObservableCollection<RutasData> Get
    {
        get { return _RutasCollection; }
    }

    /// <summary>
    /// Retorna el conjunto entero de rutas en la colección
    /// </summary>
    /// <returns></returns>
    public ObservableCollection<RutasData> GetCollection()
    {
        return _RutasCollection;
    }

    /// <summary>
    /// Añade un elemento tipo RutasData a la colección
    /// </summary>
    /// <param name="hora"></param>
    public void Add(RutasData ruta)
    { _RutasCollection.Add(ruta); }

    /// <summary>
    /// Elimina un elemento tipo RutasData de la colección
    /// </summary>
    /// <param name="ruta"></param>
    public void Remove(RutasData ruta)
    { _RutasCollection.Remove(ruta); }

    /// <summary>
    /// Elimina todos los registros de la colección
    /// </summary>
    public void RemoveAll()
    { _RutasCollection.Clear(); }

    /// <summary>
    /// Inserta un elemento tipo RutasData a la colección
    /// en la posición rowId establecida
    /// </summary>
    /// <param name="rowId"></param>
    /// <param name="ruta"></param>
    public void Insert(int rowId, RutasData ruta)
    { _RutasCollection.Insert(rowId, ruta);  }     

}

/// <summary>
/// Clase RutasData
/// </summary>
// Registro tabla interficie pantalla
public class RutasData
{
    public int Id { get; set; }
    public bool Activa { get; set; }
    public string Ruta { get; set; }        
}
}

and in my page loaded event I do this to populate gridview:

        // Obtiene datos tabla Rutas
        var tbl_Rutas = Accessor.GetRutasTable(); // This method returns entire table

        foreach (var ruta in tbl_Rutas)
        {                
            _RutasCollection.Add(new RutasData
            {
                Id = (int) ruta.Id,
                Ruta = ruta.Ruta,
                Activa = (bool) ruta.Activa
            });
        }

        // Enlaza los datos con el objeto proveedor RutasCollection
        lstViewRutas.ItemsSource = _RutasCollection.GetCollection();

Everything is ok but now I would like to check/uncheck all checkboxes in the gridviewcolumn when I press one button or another. How can I do this?

Something like this¿? I receive an error that says I can modify itemsource property.

    private void btnCheckAll_Click(object sender, RoutedEventArgs e)
    {

        // Update data object bind to gridview
        ObservableCollection<RutasData> listas = _RutasCollection.GetCollection();
        foreach (var lst in listas)
        {                
            ((RutasData)lst).Activa = true;                
        }

        // Update with new values the UI
        lstViewRutas.ItemsSource = _RutasCollection.GetCollection();            
    }

Thanks!


You dont have to check/uncheck all the checkboxes. you just set the property that the check boxes are bound to and then the check boxes will check uncheck. You do however need to implement INotifyPropertyChanged. This lets the UI know an underlying property has changed.

change the following

public class RutasData : INotifyPropertyChanged
{
    public int Id { get; set; }
    private Boolean _activa;

    /// <summary>
    /// Gets and sets the Activa property
    /// </summary>
    public Boolean Activa {
       get { return _activa; }
       set {
          if (_activa == value) { }
          else {
             _activa = value;
             NotifyPropertyChanged("Activa");
          }
       }
    }

    public string Ruta { get; set; }    


    #region INotifyPropertyChanged Members

    /// <summary>
    /// Property Changed event
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Standard NotifyPropertyChanged Method
    /// </summary>
    /// <param name="propertyName">Property Name</param>
    private void NotifyPropertyChanged(string propertyName) {
       if (PropertyChanged != null) {
          PropertyChanged(this,
            new PropertyChangedEventArgs(propertyName));
       }
    }

    #endregion    
}

now when you set the property Activa (in code) the UI will update and your checkboxes will check/uncheck

you wont need to do this

// Update with new values the UI
lstViewRutas.ItemsSource = _RutasCollection.GetCollection(); 
0

精彩评论

暂无评论...
验证码 换一张
取 消