Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
XML
<ListView x:Name="listView" Margin="83,382,487,280" ItemsSource="{Binding Path=Table}">
            <ListView.View>
                <GridView>
                   <!-- <GridViewColumn Header="Name" />-->
                    <GridViewColumn Header="Active"  >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox Tag="{Binding flat_number}"  Content="{Binding Path=flat_number}" IsChecked="{Binding Active}" Name="check1" Checked="check1_Checked"/>
                                <!-- Items........-->
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                        <GridViewColumn.HeaderTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <CheckBox  Checked="CheckBox_Checked" Unchecked="CheckBox_Checked"/><!--top-->
                                    <TextBlock VerticalAlignment="Center" Text="Select All" Margin="5,0,0,0"/>
                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.HeaderTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
            </ListView>












C#
private void CheckBox_Checked(object sender, RoutedEventArgs e)
       {
           var checkBox = sender as CheckBox;
           if (null == checkBox) return;
           foreach ( var item in list)
               item.Active = checkBox.IsChecked.Value;
       }

       public class MyClass : INotifyPropertyChanged
       {
           private string name;
          public string Name
           {
               get { return name; }
               set { name = value; ReportChange("Name"); }
           }

           private bool active;
           public bool Active
           {
               get { return active; }
               set { active = value; ReportChange("Active"); }
           }


           private void ReportChange(string propertyName)
           {

               if (null != PropertyChanged) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

           }

           #region INotifyPropertyChanged Members

           public event PropertyChangedEventHandler PropertyChanged;

           #endregion
       }
Posted
Updated 12-Oct-12 0:09am
v5

Here you can use javascript
JavaScript
  <script type="text/javascript"> 
 function HeaderClick(CheckBox) 
        {
            //Get target base & child control.
            var TargetBaseControl = document.getElementById(grvLeaveDetails);
            var TargetChildControl = "chkSelect";

            //Get all the control of the type INPUT in the base control.
            var Inputs = TargetBaseControl.getElementsByTagName("input");

            //Checked/Unchecked all the checkBoxes in side the GridView.
            for (var n = 0; n < Inputs.length; ++n)
                if (Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf(TargetChildControl, 0) >= 0)
                    Inputs[n].checked = CheckBox.checked;
            
            var TotalChkBx = document.getElementById('totRows').value;
            //Reset Counter
            document.getElementById('hiddenChkCount').value = CheckBox.checked ? TotalChkBx : 0;
         }
         
         
        function ChildClick(CheckBox, HCheckBox) 
        {
            var TotalChkBx = document.getElementById('totRows').value;
            //get target base & child control.
            var HeaderCheckBox = document.getElementById(HCheckBox);
            
            //Modifiy Counter;
            if (CheckBox.checked && document.getElementById('hiddenChkCount').value < TotalChkBx)
                document.getElementById('hiddenChkCount').value = parseInt(document.getElementById('hiddenChkCount').value) + 1;
            else if (document.getElementById('hiddenChkCount').value > 0)
                document.getElementById('hiddenChkCount').value = parseInt(document.getElementById('hiddenChkCount').value) - 1;

            //Change state of the header CheckBox.
            if (document.getElementById('hiddenChkCount').value < TotalChkBx)
            {
                HeaderCheckBox.checked = false;
            }
            else if(document.getElementById('hiddenChkCount').value == TotalChkBx)
            {
                HeaderCheckBox.checked = true;
            }
        }
</script>    


Then call this function in gridview

C#
<asp:gridview id="grvLeaveDetails" runat="server" autogeneratecolumns="false" bordercolor="Black" borderstyle="Solid" borderwidth="1px" onrowcreated="grvLeaveDetails_RowCreated" xmlns:asp="#unknown">
<columns>
   <asp:templatefield>
      <headertemplate>
          <asp:checkbox id="chkAll" runat="server" onclick="javascript:HeaderClick(this);" />
      </headertemplate>
         ItemStyle Font-Size="11px" Font-Names="Verdana,Arial" Height="20px" Width="10%" HorizontalAlign="Center" />
      <itemtemplate>
          <asp:checkbox id="chkSelect" runat="server" />
        </itemtemplate>
      </asp:templatefield>
</columns>
</asp:gridview>


Then Take Two hidden Field in aspx Page.

ASP.NET
<input type="hidden" id="hiddenChkCount" runat="server" value="0" />
   <asp:hiddenfield id="totRows" runat="server" value="0" xmlns:asp="#unknown" />



Then used blow mention code in .cs page in RowCreated Event Of grid

C#
protected void grvLeaveDetails_RowCreated(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
       {
           CheckBox chkBxSelect = (CheckBox)e.Row.Cells[0].FindControl("chkSelect");
           CheckBox chkBxHeader = (CheckBox)this.grvLeaveDetails.HeaderRow.FindControl("chkAll");

           chkBxSelect.Attributes["onclick"] = string.Format("javascript:ChildClick(this,'{0}');", chkBxHeader.ClientID);
       }
   }



Then used hidden field value in .cs page where gridview is bind example is mention blow

C#
grvLeaveDetails.DataSource = ds.Tables[0].DefaultView;
              grvLeaveDetails.DataBind();
              totRows.Value = grvLeaveDetails.Rows.Count.ToString();


Thanks,
Abhimanyu Rawat
 
Share this answer
 
v2
Comments
Dibin Babu 12-Oct-12 6:06am    
Its An ASP.net Code,I want WPF code
[no name] 12-Oct-12 10:43am    
Javascript in a WPF application? Are you serious?
public static Visual GetDescendantByType(Visual element, Type type, string name)
{
if (element == null) return null;
if (element.GetType() == type)
{
FrameworkElement fe = element as FrameworkElement;
if (fe != null)
{
if (fe.Name == name)
{
return fe;
}
}
}
Visual foundElement = null;
if (element is FrameworkElement)
(element as FrameworkElement).ApplyTemplate();
for (int i = 0;
i < VisualTreeHelper.GetChildrenCount(element); i++)
{
Visual visual = VisualTreeHelper.GetChild(element, i) as Visual;
foundElement = GetDescendantByType(visual, type, name);
if (foundElement != null)
break;
}
return foundElement;
}

private void checkBox1_Checked(object sender, RoutedEventArgs e)
{

ItemContainerGenerator generator = this.list1.ItemContainerGenerator;
for (int a = 0; a < list1.Items.Count; a++)
{
ListViewItem selectedItem = (ListViewItem)generator.ContainerFromIndex(a);
CheckBox check = GetDescendantByType(selectedItem, typeof(CheckBox), "check1") as CheckBox;
check.IsChecked = true;

}
}
private void checkBox1_UnChecked(object sender, RoutedEventArgs e)
{
ItemContainerGenerator generator = this.list1.ItemContainerGenerator;
for (int a = 0; a < list1.Items.Count; a++)
{
ListViewItem selectedItem = (ListViewItem)generator.ContainerFromIndex(a);
CheckBox check = GetDescendantByType(selectedItem, typeof(CheckBox), "check1") as CheckBox;
check.IsChecked = false;

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900