---
lang: ja-jp
breaks: true
---
# WPF DataGrid を `AutoGenerateColumns="True"` としたときに、余計な項目を削除する 2022-06-28
```xaml=
<Window.DataContext>
<viewModels:MainWindowViewModel />
</Window.DataContext>
<Grid>
<DataGrid
ItemsSource="{Binding Peoples_IEnumerable}"
AutoGenerateColumns="True"
・・・
AutoGeneratingColumn="DataGrid_AutoGeneratingColumn"
/>
</Grid>
````
力技だが、これしか思いつかない。。
```csharp=
private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
switch (e.PropertyName)
{
case "DependencyObjectType":
case "IsSealed":
case "Dispatcher":
case "XmlAttributeProperties.XmlSpace":
case "XmlAttributeProperties.XmlnsDictionary":
case "XmlAttributeProperties.XmlnsDefinition":
case "XmlAttributeProperties.XmlNamespaceMaps":
case "InputMethod.IsInputMethodEnabled":
case "InputMethod.IsInputMethodSuspended":
case "InputMethod.PreferredImeState":
case "InputMethod.PreferredImeConversionMode":
case "InputMethod.PreferredImeSentenceMode":
case "InputMethod.InputScope":
case "ToolTipService.ToolTip":
case "ToolTipService.HorizontalOffset":
case "ToolTipService.VerticalOffset":
case "ToolTipService.HasDropShadow":
case "ToolTipService.PlacementTarget":
case "ToolTipService.PlacementRectangle":
case "ToolTipService.Placement":
case "ToolTipService.ShowOnDisabled":
case "ToolTipService.IsEnabled":
case "ToolTipService.ShowDuration":
case "ToolTipService.InitialShowDelay":
case "ToolTipService.BetweenShowDelay":
case "ContextMenuService.ContextMenu":
case "ContextMenuService.HorizontalOffset":
case "ContextMenuService.VerticalOffset":
case "ContextMenuService.HasDropShadow":
case "ContextMenuService.PlacementTarget":
case "ContextMenuService.PlacementRectangle":
case "ContextMenuService.Placement":
case "ContextMenuService.ShowOnDisabled":
case "ContextMenuService.IsEnabled":
case "Stylus.IsPressAndHoldEnabled":
case "Stylus.IsFlicksEnabled":
case "Stylus.IsTapFeedbackEnabled":
case "RenderOptions.EdgeMode":
case "RenderOptions.BitmapScalingMode":
case "RenderOptions.ClearTypeHint":
case "NumberSubstitution.CultureSource":
case "NumberSubstitution.CultureOverride":
case "NumberSubstitution.Substitution":
case "Typography.StandardLigatures":
case "Typography.ContextualLigatures":
case "Typography.DiscretionaryLigatures":
case "Typography.HistoricalLigatures":
case "Typography.AnnotationAlternates":
case "Typography.ContextualAlternates":
case "Typography.HistoricalForms":
case "Typography.Kerning":
case "Typography.CapitalSpacing":
case "Typography.CaseSensitiveForms":
case "Typography.StylisticSet1":
case "Typography.StylisticSet2":
case "Typography.StylisticSet3":
case "Typography.StylisticSet4":
case "Typography.StylisticSet5":
case "Typography.StylisticSet6":
case "Typography.StylisticSet7":
case "Typography.StylisticSet8":
case "Typography.StylisticSet9":
case "Typography.StylisticSet10":
case "Typography.StylisticSet11":
case "Typography.StylisticSet12":
case "Typography.StylisticSet13":
case "Typography.StylisticSet14":
case "Typography.StylisticSet15":
case "Typography.StylisticSet16":
case "Typography.StylisticSet17":
case "Typography.StylisticSet18":
case "Typography.StylisticSet19":
case "Typography.StylisticSet20":
case "Typography.Fraction":
case "Typography.SlashedZero":
case "Typography.MathematicalGreek":
case "Typography.EastAsianExpertForms":
case "Typography.Variants":
case "Typography.Capitals":
case "Typography.NumeralStyle":
case "Typography.NumeralAlignment":
case "Typography.EastAsianWidths":
case "Typography.EastAsianLanguage":
case "Typography.StandardSwashes":
case "Typography.ContextualSwashes":
case "Typography.StylisticAlternates":
case "KeyboardNavigation.TabIndex":
case "KeyboardNavigation.IsTabStop":
case "KeyboardNavigation.TabNavigation":
case "KeyboardNavigation.ControlTabNavigation":
case "KeyboardNavigation.DirectionalNavigation":
case "KeyboardNavigation.AcceptsReturn":
case "Validation.ErrorTemplate":
case "Validation.ValidationAdornerSite":
case "Validation.ValidationAdornerSiteFor":
case "ToolTipService.IsOpen":
case "Length":
case "LongLength":
case "Rank":
case "SyncRoot":
case "IsReadOnly":
case "IsFixedSize":
case "IsSynchronized":
case "InputLanguageManager.InputLanguage":
case "InputLanguageManager.RestoreInputLanguage":
e.Cancel = true;
break;
default:
Debug.WriteLine($"-------- DataGrid_AutoGeneratingColumn {e.PropertyName} {e.PropertyType} {e.Column} {e.PropertyDescriptor}");
break;
}
}
```
###### tags: `WPF` `DataGrid` `AutoGenerateColumns`