如何在WPF中单击一个复选框时选中所有复选框。列表框中的值取自数据库(flat-number)。 [英] How to select all checkboxes in when one check box is clicked in WPF.The value in the list box is taken from database(flat-number).
本文介绍了如何在WPF中单击一个复选框时选中所有复选框。列表框中的值取自数据库(flat-number)。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<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>
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
}
推荐答案
在这里你可以使用javascript
Here you can use 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>
然后在gridview中调用此函数
Then call this function in gridview
<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>
然后在aspx页面中取两个隐藏字段。
Then Take Two hidden Field in aspx Page.
<input type="hidden" id="hiddenChkCount" runat="server" value="0" />
<asp:hiddenfield id="totRows" runat="server" value="0" xmlns:asp="#unknown" />
然后在RowCreated事件中使用.cs页面中的提及代码网格
Then used blow mention code in .cs page in RowCreated Event Of grid
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);
}
}
然后使用隐藏字段值在.cs页面中gridview是绑定示例提及打击
Then used hidden field value in .cs page where gridview is bind example is mention blow
grvLeaveDetails.DataSource = ds.Tables[0].DefaultView;
grvLeaveDetails.DataBind();
totRows.Value = grvLeaveDetails.Rows.Count.ToString();
谢谢,
Abhimanyu Rawat
Thanks,
Abhimanyu Rawat
public static Visual GetDescendantByType(视觉元素,类型类型,字符串名称)
{
if(element == null)返回null;
if (element.GetType()== type)
{
FrameworkElement fe =元素作为FrameworkElement;
if(fe!= null)<如果(fe.Name == name)
$
b $ b}
}
}
Visual foundElement = null;
if(element is FrameworkElement)
(作为FrameworkElement的元素).ApplyTemplate();
for(int i = 0;
i< VisualTreeHelper.GetChildrenCount(elemen)吨); 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)为CheckBox;
check.IsChecked = true;
}
}
private void checkBox1_UnChecked(对象发送者,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)作为CheckBox;
check.IsChecked = false;
}
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;
}
这篇关于如何在WPF中单击一个复选框时选中所有复选框。列表框中的值取自数据库(flat-number)。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文