WPF Check/Uncheck all checkboxes located in a gridview

Posted by toni on Stack Overflow See other posts from Stack Overflow or by toni
Published on 2010-03-24T21:10:14Z Indexed on 2010/03/24 21:13 UTC
Read the original article Hit count: 959

Filed under:
|
|
|
|

Hi!

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!

© Stack Overflow or respective owner

Related posts about wpf

Related posts about check