<DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{Binding IsCheck,UpdateSourceTrigger=PropertyChanged}" x:Name="DTCB" Command="{Binding DataContext.SelectedCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" CommandParameter="{Binding Cad_No}"> <CheckBox.InputBindings> <MouseBinding MouseAction="LeftClick" /> </CheckBox.InputBindings> </CheckBox> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>怎么让第一行的checkbox不显示
时间: 2023-07-19 07:07:43 浏览: 226
25.Xaml DateGrid控件->默认单选,可以多项选择的网格控件
你可以在第一行的数据模型中将 `IsCheck` 属性设置为 `false`,这样第一行的 `CheckBox` 就不会被选中并且不会显示了。或者,你可以使用 `RowStyle` 设置第一行的样式,将 `CheckBox` 隐藏:
```xml
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridRow">
<Border x:Name="DGR_Border"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="True">
<SelectiveScrollingGrid>
<SelectiveScrollingGrid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</SelectiveScrollingGrid.ColumnDefinitions>
<SelectiveScrollingGrid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</SelectiveScrollingGrid.RowDefinitions>
<!-- Hide CheckBox in the first row -->
<CheckBox x:Name="DTCB"
IsChecked="{Binding IsCheck, UpdateSourceTrigger=PropertyChanged}"
Command="{Binding DataContext.SelectedCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"
CommandParameter="{Binding Cad_No}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Visibility="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Converter={StaticResource FirstRowToVisibilityConverter}, ConverterParameter={Binding}}"/>
<DataGridCellsPresenter Grid.Column="1"
ItemsPanel="{TemplateBinding ItemsControl.ItemsPanel}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</SelectiveScrollingGrid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
```
你还需要实现一个 `FirstRowToVisibilityConverter` 转换器来将第一行的可见性设置为 `Collapsed`,其他行的可见性设置为 `Visible`:
```csharp
public class FirstRowToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is CollectionView collectionView && collectionView.Count > 0)
{
var firstItem = collectionView.GetItemAt(0);
return firstItem == parameter ? Visibility.Collapsed : Visibility.Visible;
}
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
```
阅读全文