小弟我用的是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高手

解决方案 »

  1.   

    windform和webform的就不要说了,很多属性不一样,不能用!
      

  2.   

    DataGridRow datagridrow = dgExam.ItemContainerGenerator.ContainerFromItem(dgExam.SelectedItem) as DataGridRow;
    CheckBox checkbox1 = AllowCheckbox.GetCellContent(datagridrow) as CheckBox;
                if (checkbox1.IsChecked == true)
                {
                    MessageBox.Show("11");
                }
                else
                {
                    MessageBox.Show("22");
                }
    这可以判断在SelectionChanged事件中有无选中该行的checkbox
      

  3.   

    foreach (object o in dgExam.ItemsSource)
                {
                    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 时抛出“未将对象引用设置到对象的实例”的异常。求高手
      

  4.   


    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; }
        }
    }
      

  5.   

     private void MultipleChoice()
            {
                _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());
                    }
                }
            }
      

  6.   

    楼上说的貌似不对吧?当数据很多的时候,就会出问题。假设说,你有200条数据,而你的窗口是盛不下200条数据的,也就是说会有一部分数据超出窗口的显示范围,那么当用楼上的方法遍历数据的时候,一旦遍历到200条数据中超出屏幕范围的数据就会出错。或者说,我们把DataGrid的滚动条往下拉,使DataGrid的第一条数据远远地超出窗口的显示范围,那么遍历时第一条数据就会出错。再或者,当我们把窗口调小点的话这种情况更容易发生。请求解法!!!
      

  7.   

    wpf数据控件就不要去操作控件了,直接数据源判断不就行了.
    比如你绑定的数据源,建个BOOL类型列或属性,直接绑到你的checkbox控件好了,循环就循环你的数据源。
    TreeView更是应该如此,降低程序判断难度
      

  8.   

    up,我也遇到你这样的问题,在csdn挂了N天哇,米人回答,
    微软的ListView
    两个方法
    ListViewItem lstitem = (ListViewItem)this.lstAttributes.ItemContainerGenerator.ContainerFromItem(item);ListViewItem lstitem = (ListViewItem)this.lstAttributes.ItemContainerGenerator.ContainerFromIndex(index);返回的ListViewItem 对象老是为空。,,,