控件
<DataGrid Width="1313" Height="749" IsReadOnly="True" BorderBrush="#BCBCBC" Margin="0 6 0 0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserResizeRows="False"
HorizontalAlignment="Left"
VerticalAlignment="Top"
CanUserAddRows="False"
ItemsSource="{Binding Warehouses}" SelectionMode="Single" SelectedItem="{Binding SelectWarehouse, Mode=TwoWay}" Background="Transparent" RowHeaderWidth="0" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden"
AutoGenerateColumns="False" AlternatingRowBackground="#E0E8F3">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="Background" Value="#3C79D4" />
<Setter Property="FontSize" Value="15" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Height" Value="28" />
<Setter Property="Width" Value="219" />
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Resources>
<Style TargetType="DataGridCell">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#0078D7"/>
</Style.Resources>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Black"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="库位ID">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Style="{DynamicResource StackStyle}" >
<TextBlock Text="{Binding Path=Code}"></TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="材料种类">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Style="{DynamicResource StackStyle}" >
<TextBlock Text="{Binding Path=Material}"></TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="材料名称">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Style="{DynamicResource StackStyle}" >
<TextBlock Text="{Binding Path=Material}"></TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="库位满">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Style="{DynamicResource StackStyle}" >
<TextBlock Text="{Binding Path=Max}"></TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="库位低">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Style="{DynamicResource StackStyle}" >
<TextBlock Text="{Binding Path=Min}"></TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="启用状态">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Style="{DynamicResource StackStyle}" >
<TextBlock Text="{Binding Path=Status,Converter={ StaticResource enableConverter}}"></TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand Command="{Binding SelectedStoreCmd}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
样式
<Style x:Key="StackStyle" TargetType="StackPanel">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Height" Value="30"/>
<Setter Property="TextBlock.FontSize" Value="24"/>
</Style>
这段代码展示了一个WPF DataGrid的详细配置,包括禁用编辑、排序、大小调整功能,设置背景透明,单选模式,以及列头和单元格的样式。列头颜色为蓝色,字体大小15,前景色白色。单元格在被选中时前景色为黑色。DataGrid的数据源绑定到Warehouses,选中项绑定到SelectWarehouse。当选择项改变时触发SelectedStoreCmd命令。此外,定义了StackPanel样式并应用于单元格模板。
1572

被折叠的 条评论
为什么被折叠?



