美好的一天。
请创建一个类似于此屏幕截图的数据网格
我找到了一些指导如何进行相似操作的代码,但是我对实现有些迷惑。
这是我要编辑的代码
<DataGrid ItemsSource="{Binding }" Name="dataGrid1" AutoGenerateColumns="False"
ColumnHeaderHeight="50" >
<DataGrid.Resources>
<Style x:Key="DataGridColumnHeader" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Grid>
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Main1" Width="60"
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Width="60"/>
<DataGridColumnHeader Content="Nested1" Width="60"/>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Header="Nested2" Width="60"
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock FontWeight="Bold" Text="Main1"/>
<DataGridColumnHeader Content="Nested2" Width="60" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Width="60"
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock />
<DataGridColumnHeader Content="Nested3" Width="60"/>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
我为此修改了代码
<DataGrid Grid.Column="2"
Grid.Row="1" Grid.RowSpan="2"
Height="100"
x:Name="assetListGrid"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
Margin="10 10 20 20"
Background="{StaticResource ForegroundLightBrush}"
LoadingRow="AddIndexNumberWhenLoadingRow"
BorderThickness="1"
VerticalScrollBarVisibility="Auto"
AutoGenerateColumns="False"
ItemsSource="{Binding AssetList, Mode=TwoWay}"
>
<DataGrid.Resources>
<Style x:Key="DataGridColumnHeader" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Background" Value="{StaticResource WordBlueBrush}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Grid Background="{StaticResource WordBlueBrush}">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Asset"
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock FontSize="{StaticResource FontSizeLarge}"
FontFamily="{StaticResource LatoBold}"
/>
<DataGridColumnHeader Content="Asset Number"/>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Header="Name"
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock FontSize="{StaticResource FontSizeLarge}"
FontFamily="{StaticResource LatoBold}" Text="Asset"/>
<DataGridColumnHeader Content="Name" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock />
<DataGridColumnHeader Content="Role" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock />
<DataGridColumnHeader Content="Comment" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock />
<DataGridColumnHeader Content="Creation TimeStamp" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
</DataGrid.Columns>
<DataGridTextColumn
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock />
<DataGridColumnHeader Content="Location" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock />
<DataGridColumnHeader Content="Value" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
</DataGrid>
这是结果
我不知道如何添加下一个顶部列(制造商及其子列) “资产”的子列看起来像个演出,我不知道该如何均衡。我试图将相同的高度添加到所有列标题都无济于事。
此外,我还发现很难修改单元格模板并将其绑定到视图模型中的特定属性。这是因为某些单元格将同时包含文本框和按钮。
任何人都能善良地指导我如何做
enter code here
答案 0 :(得分:0)
这不是答案,但可能会有所帮助:
<DataGrid.Columns>
<DataGridTextColumn Header="1">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<Grid MinWidth="200">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="header" Grid.ColumnSpan="3" HorizontalAlignment="Center"></TextBlock>
<TextBlock Grid.Row="1" Text="col"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="col1"/>
<TextBlock Grid.Row="1" Grid.Column="2" Text="col2"/>
</Grid>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Header="2"></DataGridTextColumn>
</DataGrid.Columns>
答案 1 :(得分:0)
这是几个问题的答案
角色,注释,创建时间戳记列上缺少字体
FontSize="{StaticResource FontSizeLarge}" FontFamily="{StaticResource LatoBold}"
对于下一组列,您只需遵循相同的设计模式
<DataGrid Grid.Column="2"
Grid.Row="1" Grid.RowSpan="2"
Height="100"
x:Name="assetListGrid"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
Margin="10 10 20 20"
BorderThickness="1"
VerticalScrollBarVisibility="Auto"
AutoGenerateColumns="False"
ItemsSource="{Binding AssetList, Mode=TwoWay}"
>
<DataGrid.Resources>
<Style x:Key="DataGridColumnHeader" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Grid >
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Asset"
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock/>
<DataGridColumnHeader Content="Asset Number"/>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Header="Name"
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="Asset"/>
<DataGridColumnHeader Content="Name" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock />
<DataGridColumnHeader Content="Role" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock />
<DataGridColumnHeader Content="Comment" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock />
<DataGridColumnHeader Content="Creation TimeStamp" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Header="Asset"
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock/>
<DataGridColumnHeader Content="Asset Number"/>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Header="Name"
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="Manufacturer "/>
<DataGridColumnHeader Content="Name" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock />
<DataGridColumnHeader Content="Role" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock />
<DataGridColumnHeader Content="Comment" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock />
<DataGridColumnHeader Content="Creation TimeStamp" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
</DataGrid.Columns>
<DataGridTextColumn
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock />
<DataGridColumnHeader Content="Location" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock />
<DataGridColumnHeader Content="Value" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
</DataGrid>
希望有帮助。
答案 2 :(得分:0)
这是我的datagrid的完整实现
<DataGrid Grid.Column="2"
Grid.Row="1" Grid.RowSpan="2"
x:Name="assetListGrid"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
Margin="10 10 20 20"
Background="{StaticResource ForegroundLightBrush}"
LoadingRow="AddIndexNumberWhenLoadingRow"
BorderThickness="1"
VerticalScrollBarVisibility="Auto"
AutoGenerateColumns="False"
ItemsSource="{Binding AssetList, Mode=TwoWay}"
>
<DataGrid.Resources>
<Style x:Key="DataGridColumnHeader" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="VerticalContentAlignment" Value="Center" />
<!--<Setter Property="Background" Value="{StaticResource WordRedBrush}" />-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Grid Background="{StaticResource WordBlueBrush}" >
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="DatagridColumnHeader2" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="BorderBrush" Value="{StaticResource ForegroundLightBrush}"/>
<Setter Property="BorderThickness" Value="0 0 1 0" />
<Setter Property="Background" Value="{StaticResource WordOrangeBrush}" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="Height" Value="30" />
<Setter Property="FontSize" Value="{StaticResource FontSizeLarge}" />
<Setter Property="Foreground" Value="{StaticResource ForegroundLightBrush}" />
</Style>
</DataGrid.Resources>
<!-- Columns -->
<DataGrid.Columns>
<!-- Asset Properties -->
<!-- Asset Number-->
<DataGridTemplateColumn Header="Asset"
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock
FontSize="{StaticResource FontSizeXLarge}"
FontFamily="{StaticResource LatoBold}"
HorizontalAlignment="Center"
Height="40"
/>
<DataGridColumnHeader
HorizontalContentAlignment="Center"
Width="250" Content="Asset Number"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBox Width="90" IsReadOnly="True"
Text="{Binding Name}"
Background="{StaticResource ForegroundLightBrush}"
Style="{StaticResource DataGridTextBox}"/>
<Button Margin="5 0 0 13" HorizontalAlignment="Right"
VerticalAlignment="Center"
Content="Right Click"
Style="{StaticResource ListViewButton}" >
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Add Critical Point" />
</ContextMenu>
</Button.ContextMenu>
</Button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- Name -->
<DataGridTemplateColumn Header="Name"
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock FontSize="{StaticResource FontSizeLarge}"
FontFamily="{StaticResource LatoBold}"
Height="40"
MinWidth="60"
Width="auto"
x:Name="nameHeader"
Text="Asset"/>
<DataGridColumnHeader Width="{Binding ActualWidth, ElementName=nameHeader}" HorizontalContentAlignment="Center"
Content="Name" MaxWidth="150" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox MaxWidth="150"
Style="{StaticResource DataGridTextBox}"
/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- Role -->
<DataGridTemplateColumn
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock FontSize="{StaticResource FontSizeLarge}"
FontFamily="{StaticResource LatoBold}"
Height="40"
Width="auto"
MinWidth="45"
MaxWidth="150"
x:Name="roleHeader"
/>
<DataGridColumnHeader HorizontalContentAlignment="Center" Width="{Binding Path=ActualWidth, ElementName=roleHeader}"
MaxWidth="150" Content="Role" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox MaxWidth="150"
Style="{StaticResource DataGridTextBox}"
/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- Critical Point-->
<!-- CP Number-->
<DataGridTemplateColumn Header="Critical Point"
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock
FontSize="{StaticResource FontSizeLarge}"
FontFamily="{StaticResource LatoBold}"
HorizontalAlignment="Center"
Height="40"
Text="Critical Point"
Padding="0 3 0 0"
/>
<DataGridColumnHeader Style="{StaticResource DatagridColumnHeader2}"
Width="250" Content="CP Number"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Width="100" IsReadOnly="True"
Text="{Binding CriticalPoint.CPNumber}"
Background="{StaticResource ForegroundLightBrush}"
Style="{StaticResource DataGridTextBox}"/>
<Button Margin="5 0 0 13" HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="Right Click"
Style="{StaticResource ListViewButton}" >
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Delete Critical Point" />
</ContextMenu>
</Button.ContextMenu>
</Button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--Name-->
<DataGridTemplateColumn
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock FontSize="{StaticResource FontSizeLarge}"
FontFamily="{StaticResource LatoBold}"
Height="40"
Width="auto"
MinWidth="60"
MaxWidth="150"
x:Name="commentHeader"
/>
<DataGridColumnHeader Style="{StaticResource DatagridColumnHeader2}" Width="{Binding Path=ActualWidth, ElementName=commentHeader}"
MaxWidth="150" Content="Name" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox MaxWidth="150"
Background="{StaticResource ForegroundLightBrush}"
Style="{StaticResource DataGridTextBox}"
/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--Role-->
<DataGridTemplateColumn
HeaderStyle="{StaticResource DataGridColumnHeader}">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock FontSize="{StaticResource FontSizeLarge}"
FontFamily="{StaticResource LatoBold}"
Height="40"
Width="auto"
MinWidth="60"
MaxWidth="150"
x:Name="commentHeader"
/>
<DataGridColumnHeader Style="{StaticResource DatagridColumnHeader2}" Width="{Binding Path=ActualWidth, ElementName=commentHeader}"
MaxWidth="150" Content="Role" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox MaxWidth="150"
Background="{StaticResource ForegroundLightBrush}"
Style="{StaticResource DataGridTextBox}"
/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>