下面是我的DataGrid
-xaml,它绑定到一个数据源。
<DataGrid AutoGenerateColumns="False" Height="240" Name="dtDemo"
ItemsSource="{Binding Path=LfdConnections}">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight" Value="Bold" />
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Resources>
<ResourceDictionary>
<Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Monitor" Width="55" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Path=MonitorName}" Height="28" Name="lblMonitorName" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Connection" Width="70" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=ConnTypes,Mode=OneTime, UpdateSourceTrigger=PropertyChanged}" SelectedIndex="0">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Port/IP" Width="70" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=PortTypes,Mode=OneTime, UpdateSourceTrigger=PropertyChanged}"
SelectedIndex="0"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="ID" Width="50" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=LfdIds,Mode=OneTime, UpdateSourceTrigger=PropertyChanged}"
SelectedIndex="0"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Source" Width="130" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=SourceTypes,Mode=OneTime, UpdateSourceTrigger=PropertyChanged}"
SelectedIndex="0"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Connect" Width="*" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Connect" Height="23" Name="btnConnect" Width="75" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我想在这个DataGrid
中添加一个image
作为列标题。你能告诉我怎么做吗?
请尝试以下片段
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<Grid>
<Image Source="C:UsersVimalDesktop1.jpg" />
<TextBlock Text="Monitor" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</DataGridTemplateColumn.Header>
</DataGridTemplateColumn>
这是一个很老的问题,但如果它对某人有用,我必须对Vimal的答案进行一些更改。这是我的代码:
<DataGridTemplateColumn Header="EstadoSinc" CanUserSort="False" MinWidth="60" Width="60" MaxWidth="60" >
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<Image Source="/MyApp;component/Images/Sincronizar.png" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Path=IdEstadoSincronizacion}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>