wpf 从 DataGrid 的 DataGridTemplateColumn 中获取复选框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12880335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:46:10  来源:igfitidea点击:

Get Checkbox Value From DataGridTemplateColumn of DataGrid

c#wpf

提问by Raika

I have this XAML

我有这个 XAML

<DataGrid Name="grdData" ... >
   <DataGrid.Columns>
      <DataGridTemplateColumn Header="Something">
         <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
               <CheckBox Name="chb" />
            </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
       </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

i try this code to get Checked State

我尝试使用此代码来获取已检查状态

for( int i = 0 ; i < grdData.Items.Count ; i++ )
{
    DataGridRow row = ( DataGridRow )grdData.ItemContainerGenerator.ContainerFromIndex( i );
    var cellContent = grdData.Columns[ 1 ].GetCellContent( row ) as CheckBox;
    if( cellContent != null && cellContent.IsChecked == true )
    {
       //some code
    }
}

my code is wrong?

我的代码错了?

回答by Rohit Vats

Since you are looping over the Itemscollection which is your ItemsSource. Why not to have the bool propertyin your class itself and get it from there itself.

由于您正在遍历Items属于您的ItemsSource. 为什么不在bool property您的班级中拥有并从那里获得它。

Say if ItemSource is List<Person>, then create a bool property say IsManagerin class Personand bind it with your checkBox like this -

假设 ItemSource is List<Person>,然后IsManager在类中创建一个 bool 属性Person并像这样将其与复选框绑定 -

<CheckBox IsChecked="{Binding IsManager}"/>

Now you can loop over the Items to get the value like this -

现在您可以遍历 Items 以获取这样的值 -

foreach(Person p in grdData.ItemsSource)
{
   bool isChecked = p.IsManager; // Tells whether checkBox is checked or not
}

EDIT

编辑

In case you can't create a property, i would suggest to use the VisualTreeHelpermethods to find the control. Use this method to find the child (Maybe you can place this in some utility class and use it, since its generic) -

如果您无法创建属性,我建议使用这些VisualTreeHelper方法来查找控件。使用此方法查找子项(也许您可以将其放在某个实用程序类中并使用它,因为它是通用的)-

public static T FindChild<T>(DependencyObject parent, string childName)
           where T : DependencyObject
{
   // Confirm parent is valid.  
   if (parent == null) return null;

   T foundChild = null;

   int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
   for (int i = 0; i < childrenCount; i++)
   {
      var child = VisualTreeHelper.GetChild(parent, i);
      // If the child is not of the request child type child 
      T childType = child as T;
      if (childType == null)
      {
          // recursively drill down the tree 
          foundChild = FindChild<T>(child, childName);

          // If the child is found, break so we do not overwrite the found child.  
          if (foundChild != null) break;
      }
      else if (!string.IsNullOrEmpty(childName))
      {
          var frameworkElement = child as FrameworkElement;
          // If the child's name is set for search 
          if (frameworkElement != null && frameworkElement.Name == childName)
          {
             // if the child's name is of the request name 
             foundChild = (T)child;
             break;
          }
      }
      else
      {
          // child element found. 
          foundChild = (T)child;
          break;
       }
   }
   return foundChild;
}

Now use the above method to get the state of your checkbox -

现在使用上述方法获取复选框的状态 -

for (int i = 0; i < grd.Items.Count; i++)
{
   DataGridRow row = (DataGridRow)grd.ItemContainerGenerator.ContainerFromIndex(i);
   CheckBox checkBox = FindChild<CheckBox>(row, "chb");
   if( checkBox != null && checkBox.IsChecked == true )
   {
       //some code
   }
}