小弟我用的是WPF,WPF中没有DataGrid,我用的是toolkit中的DataGrid,
在页面中用了toolkit/DataGrid中的DataGridCheckBoxColumn用于多选,
现在想遍历一下DataGrid,看看DataGridCheckBoxColumn列中有哪些选中了,
再把与之相对应选中的CODE拿出来,求方法或代码。
XAML代码
<Custom:DataGrid BorderBrush="Chocolate" Height="486.163" Width="397" Name="dgExam"
AutoGenerateColumns="False"
ColumnHeaderStyle="{StaticResource dgHeaderStyle}"
RowStyle="{StaticResource dgRowStyle}"
CellStyle="{StaticResource dgCellStyle}"
SelectionChanged="dgExam_SelectionChanged"
CanUserAddRows="False"
CanUserResizeRows="False"
IsReadOnly="True" SelectionMode="Extended" >
<Custom:DataGrid.Columns>
<Custom:DataGridCheckBoxColumn x:Name="AllowCheckbox" Header="选择" Width="30" Binding="{Binding ex_no}" ElementStyle="{DynamicResource NoBorderCheckBoxStyle}"
EditingElementStyle="{DynamicResource NoBorderCheckBoxStyle}" IsThreeState="True"> </Custom:DataGridCheckBoxColumn>
</Custom:DataGrid.Columns>
</Custom:DataGrid>Binding="{Binding ex_no}" 中ex_no是我绑定了数据库中的字段,dgExam.itemssource = datatable;求WPF高手
在页面中用了toolkit/DataGrid中的DataGridCheckBoxColumn用于多选,
现在想遍历一下DataGrid,看看DataGridCheckBoxColumn列中有哪些选中了,
再把与之相对应选中的CODE拿出来,求方法或代码。
XAML代码
<Custom:DataGrid BorderBrush="Chocolate" Height="486.163" Width="397" Name="dgExam"
AutoGenerateColumns="False"
ColumnHeaderStyle="{StaticResource dgHeaderStyle}"
RowStyle="{StaticResource dgRowStyle}"
CellStyle="{StaticResource dgCellStyle}"
SelectionChanged="dgExam_SelectionChanged"
CanUserAddRows="False"
CanUserResizeRows="False"
IsReadOnly="True" SelectionMode="Extended" >
<Custom:DataGrid.Columns>
<Custom:DataGridCheckBoxColumn x:Name="AllowCheckbox" Header="选择" Width="30" Binding="{Binding ex_no}" ElementStyle="{DynamicResource NoBorderCheckBoxStyle}"
EditingElementStyle="{DynamicResource NoBorderCheckBoxStyle}" IsThreeState="True"> </Custom:DataGridCheckBoxColumn>
</Custom:DataGrid.Columns>
</Custom:DataGrid>Binding="{Binding ex_no}" 中ex_no是我绑定了数据库中的字段,dgExam.itemssource = datatable;求WPF高手
CheckBox checkbox1 = AllowCheckbox.GetCellContent(datagridrow) as CheckBox;
if (checkbox1.IsChecked == true)
{
MessageBox.Show("11");
}
else
{
MessageBox.Show("22");
}
这可以判断在SelectionChanged事件中有无选中该行的checkbox
{
CheckBox checkbox = dgExam.Columns[0].GetCellContent(o).FindName("AllowCheckbox") as CheckBox;
if (checkbox != null)
{
if (checkbox.IsChecked == true)
{
MessageBox.Show("asd");
}
}
else
{
MessageBox.Show("11");
}
}
这中方法在 foreach 时抛出“未将对象引用设置到对象的实例”的异常。求高手
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;namespace SLAPPDemo
{
public partial class Page : UserControl
{
public static List<ListInformation> pageItems = null; public Page()
{
InitializeComponent();
DataBind();
} /// <summary>
/// 设置 DataGrid 的数据源
/// </summary>
private void DataBind()
{
this.dataGrid.ItemsSource = GetSpecifiedPage();
} /// <summary>
/// 获取数据源
/// </summary>
private List<ListInformation> GetSpecifiedPage()
{
var source = new SourceData();
var totalList = source.GetData(); pageItems = new List<ListInformation>();
foreach (SourceDataModel sdm in totalList)
{
ListInformation li = new ListInformation();
li.Name = sdm.Name;
li.Male = sdm.Male;
li.DayOfBirth = sdm.DayOfBirth;
li.Age = sdm.Age;
li.Allow = false;
pageItems.Add(li);
}
return pageItems;
}
private void btnGetChecked_Click(object sender, RoutedEventArgs e)
{
foreach (ListInformation lst in pageItems)
{
if (lst.Allow)
MessageBox.Show(lst.Name);
}
}
} public class SourceDataModel
{
public string Name { get; set; }
public int Age { get; set; }
public DateTime DayOfBirth { get; set; }
public bool Male { get; set; }
} public class SourceData
{
public List<SourceDataModel> GetData()
{
var source = new List<SourceDataModel>(); for (int i = 0; i < 10; i++)
{
source.Add(new SourceDataModel
{
Name = "Name" + i.ToString().PadLeft(4, '0'),
Age = new Random(i).Next(20, 60),
DayOfBirth = DateTime.Now,
Male = Convert.ToBoolean(i % 2)
});
} return source;
}
} public class ListInformation
{
public string Name { get; set; }
public int Age { get; set; }
public DateTime DayOfBirth { get; set; }
public bool Male { get; set; }
public bool Allow { get; set; }
}
}
{
_amendParameters = new List<string>();
for (int i = 0; i < dgExam.Items.Count; i++)
{
CheckBox check = dgExam.Columns[0].GetCellContent(dgExam.Items[i]) as CheckBox;
if (check.IsChecked == true)
{
_amendParameters.Add(_itemstable.Rows[i]["ex_no"].ToString());
}
}
}
比如你绑定的数据源,建个BOOL类型列或属性,直接绑到你的checkbox控件好了,循环就循环你的数据源。
TreeView更是应该如此,降低程序判断难度
微软的ListView
两个方法
ListViewItem lstitem = (ListViewItem)this.lstAttributes.ItemContainerGenerator.ContainerFromItem(item);ListViewItem lstitem = (ListViewItem)this.lstAttributes.ItemContainerGenerator.ContainerFromIndex(index);返回的ListViewItem 对象老是为空。,,,