I have a DataTemplate which suppose to give me this kind of output:
when using this code:
<DataTemplate x:DataType="data:MenuItem" x:Key="MenuItemTemplate">
<StackPanel>
<Viewbox Margin="10 10 10 2">
<TextBlock FontFamily="Segoe MDL2 Assets" Text="{x:Bind Icon}" />
</Viewbox>
<TextBlock HorizontalAlignment="Center" Text="{x:Bind Name}" />
</StackPanel>
</DataTemplate>
but ... I get no Icon:
I think is something wrong in my binding, but I dont know what.
The menu item model is like this:
public class MenuItem
{
public MenuItem(string name, string icon)
{
Name = name;
Icon = $"&#x{icon};";
}
public string Name { get; set; }
public string Icon { get; set; }
}
If I just provide the icon in plain text, like this:
<DataTemplate x:DataType="data:MenuItem" x:Key="MenuItemTemplate">
<StackPanel>
<Viewbox Margin="10 10 10 2">
<TextBlock FontFamily="Segoe MDL2 Assets" Text="" />
</Viewbox>
<TextBlock HorizontalAlignment="Center" Text="{x:Bind Name}" />
</StackPanel>
</DataTemplate>
the icon is displayed correctly, but when using binding not.
Can you please help me understand what is wrong in my approach?
Edit : added full code example
public sealed partial class MainPage : Page
{
private ObservableCollection<MenuItem> MenuItems;
public MainPage()
{
this.InitializeComponent();
MenuItems = new ObservableCollection<MenuItem>(new List<MenuItem>
{
new MenuItem("Home","E913"),
new MenuItem("Volume","EC3F"),
new MenuItem("Scrap","E74C"),
new MenuItem("Report","ED25"),
});
}
}
public class MenuItem
{
public MenuItem(string name, string icon)
{
Name = name;
Icon = $"&#x{icon};";
}
public string Name { get; set; }
public string Icon { get; set; }
}
and xalm code:
<Page.Resources>
<DataTemplate x:DataType="data:MenuItem" x:Key="MenuItemTemplate">
<StackPanel>
<Viewbox Margin="10 10 10 2">
<TextBlock FontFamily="Segoe MDL2 Assets" Text="{x:Bind Icon}" />
</Viewbox>
<TextBlock HorizontalAlignment="Center" Text="{x:Bind Name}" />
</StackPanel>
</DataTemplate>
</Page.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="90" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ListBox Grid.Column="0" ItemsSource="{x:Bind MenuItems}"
ItemTemplate="{StaticResource MenuItemTemplate}" />
</Grid>

