最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Adding FontAwesome icons to data grid row in WPF code behind using C# - Stack Overflow

programmeradmin3浏览0评论

I have a datagrid with rows begin added programatically, and my datagrid in xaml has 5 columns, and each column style is as shown here:

<DataGrid.Columns>
    <DataGridTextColumn Width="Auto" Header="Status"  
         FontFamily="Arial" Foreground="Gray" FontWeight="Normal"  
         MinWidth="250" Binding="{Binding Path=Date}" >
        <DataGridTextColumn.HeaderTemplate>
            <DataTemplate>
                <StackPanel>
                    <fa:ImageAwesome Icon="Sort" Height="10" Name="Search" Margin="10,15,25,0" HorizontalAlignment="Center"   />

                    <!--<TextBlock FontFamily="FontAwesome" FontSize="16" Text="&#xf0dc;"  />-->
                    <TextBlock Text="Date"  Margin="0,-30,0,0"  />
                </StackPanel>
            </DataTemplate>
        </DataGridTextColumn.HeaderTemplate>

        <DataGridTextColumn.ElementStyle>
            <Style>
                <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
                <Setter Property="FrameworkElement.Margin" Value="0,8,0,0"></Setter>
            </Style>
        </DataGridTextColumn.ElementStyle>

        <DataGridTextColumn.HeaderStyle>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="HorizontalContentAlignment" Value="Center" />
                <Setter Property="Background" Value="#dec15a" />
                <Setter Property="Height" Value="50" />
                <Setter Property="FontWeight" Value="Bold" />
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="#d5b024" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </DataGridTextColumn.HeaderStyle>
    </DataGridTextColumn>
</DataGrid.Columns>

How can I add a font awesome success icon next to the text like below when the rows is adding from code behind

   Date      | File| Status
   03/14/2025|.pdf | Success (here the FontAwesome success icon should be shown)

Code-behind code is:

   dgrd_ListFiles.Items.Add(new Item() { Date = 
   System.DateTime.Now.ToString("MM/dd /yyyy"), File=".pdf", Status = "Success" });

I have a datagrid with rows begin added programatically, and my datagrid in xaml has 5 columns, and each column style is as shown here:

<DataGrid.Columns>
    <DataGridTextColumn Width="Auto" Header="Status"  
         FontFamily="Arial" Foreground="Gray" FontWeight="Normal"  
         MinWidth="250" Binding="{Binding Path=Date}" >
        <DataGridTextColumn.HeaderTemplate>
            <DataTemplate>
                <StackPanel>
                    <fa:ImageAwesome Icon="Sort" Height="10" Name="Search" Margin="10,15,25,0" HorizontalAlignment="Center"   />

                    <!--<TextBlock FontFamily="FontAwesome" FontSize="16" Text="&#xf0dc;"  />-->
                    <TextBlock Text="Date"  Margin="0,-30,0,0"  />
                </StackPanel>
            </DataTemplate>
        </DataGridTextColumn.HeaderTemplate>

        <DataGridTextColumn.ElementStyle>
            <Style>
                <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
                <Setter Property="FrameworkElement.Margin" Value="0,8,0,0"></Setter>
            </Style>
        </DataGridTextColumn.ElementStyle>

        <DataGridTextColumn.HeaderStyle>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="HorizontalContentAlignment" Value="Center" />
                <Setter Property="Background" Value="#dec15a" />
                <Setter Property="Height" Value="50" />
                <Setter Property="FontWeight" Value="Bold" />
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="#d5b024" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </DataGridTextColumn.HeaderStyle>
    </DataGridTextColumn>
</DataGrid.Columns>

How can I add a font awesome success icon next to the text like below when the rows is adding from code behind

   Date      | File| Status
   03/14/2025|.pdf | Success (here the FontAwesome success icon should be shown)

Code-behind code is:

   dgrd_ListFiles.Items.Add(new Item() { Date = 
   System.DateTime.Now.ToString("MM/dd /yyyy"), File=".pdf", Status = "Success" });
Share Improve this question edited Mar 17 at 18:00 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 17 at 12:51 Vybhavi RVybhavi R 12 bronze badges 1
  • I have used <DataGridTextColumn> for creating columns – Vybhavi R Commented Mar 17 at 13:08
Add a comment  | 

1 Answer 1

Reset to default 0

I think the following should work in your case. Modify the DataGridTextColumn.ElementStyle tag as below:

<DataGridTextColumn.ElementStyle>
    <Style>
        <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
        <Setter Property="FrameworkElement.Margin" Value="0,8,0,0"/>
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding}" />
                        <fa:ImageAwesome Icon="CheckCircle" Height="16" Width="16" Margin="5,0,0,0" Foreground="Green" />
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</DataGridTextColumn.ElementStyle>
发布评论

评论列表(0)

  1. 暂无评论