I'm creating a C# WPF application in which I'm programmatically creating buttons for each category stored in SQLite database. Each button contains a Font Awesome icon and text.
Table
Creating buttons
public static void LoadSidePanelButtons(StackPanel stackPanel)
{
DataTable NavigationTable = new DataTable();
string query = "SELECT * FROM Navigation ORDER BY Name ASC";
DatabaseConnection.PopulateDataTable(NavigationTable, query);
foreach (DataRow row in NavigationTable.Rows)
{
ToggleButton toggleButton = new ToggleButton();
toggleButton.Name = "id" + row["navigationID"].ToString();
toggleButton.Content = row["Name"].ToString();
toggleButton.Tag = row["Icon"].ToString();
toggleButton.Style = (Style)stackPanel.Resources["SidePanelButton"];
toggleButton.Click += new RoutedEventHandler(MainWindow.sidePanelButton_onClick);
stackPanel.RegisterName(toggleButton.Name, toggleButton);
stackPanel.Children.Add(toggleButton);
}
}
Button template
<StackPanel.Resources>
<Style x:Key="SidePanelButton" TargetType="ToggleButton">
<Setter Property="Height" Value="50" />
<Setter Property="Width" Value="230" />
<Setter Property="Margin" Value="0 0 0 10" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Border x:Name="Border" CornerRadius="3" Background="Transparent">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60" />
<ColumnDefinition Width="170" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock x:Name="Icon" Grid.Column="0" Grid.Row="0" FontSize="18" Foreground="#FF8B939C" FontFamily="{StaticResource FontAwesomeRegular}" HorizontalAlignment="Center" VerticalAlignment="Center">
<ContentPresenter x:Name="IconContent" Content="{TemplateBinding Tag}" />
</TextBlock>
<TextBlock x:Name="Title" Grid.Column="1" Grid.Row="0" FontSize="14" Foreground="#FF8B939C" FontFamily="{StaticResource Roboto}" HorizontalAlignment="Left" VerticalAlignment="Center">
<ContentPresenter x:Name="TitleContent" Content="{TemplateBinding Content}" />
</TextBlock>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" TargetName="Border" Value="#FF19BDD2" />
<Setter Property="Foreground" TargetName="Icon" Value="#FFFFFF" />
<Setter Property="Foreground" TargetName="Title" Value="#FFFFFF" />
<Setter TargetName="spbtBorder" Property="Border.Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="14" Color="Black" Opacity="0.25" ShadowDepth="4" Direction="-90" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="Border" Value="#FF19BDD2" />
<Setter Property="Foreground" TargetName="Icon" Value="#FFFFFF" />
<Setter Property="Foreground" TargetName="Title" Value="#FFFFFF" />
<Setter TargetName="Border" Property="Border.Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="14" Color="Black" Opacity="0.25" ShadowDepth="4" Direction="-90" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</StackPanel.Resources>
The problem is:
toggleButton.Tag = row["Icon"].ToString();
When I use this to add the icon, the buttons display "\uf022" as plain text.
But...
toggleButton.Tag = "\uf022";
When I change it to this, it shows the icon without any problem.
And I currently can't find the reason why unicode loaded from DB to string remains as a plain text but a manually created string with unicode appears as an icon.