نحوه نمایش تمام آیکونهای تعریف شده در یک قلم در WPF
نویسنده: وحید نصیری
تاریخ: ۱۳۹۲/۰۱/۰۱ ۱۰:۵۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace CrMap.Models
{
public class Symbol
{
public char Character { set; get; }
public string CharacterCode { set; get; }
}
}using System;
using System.Collections.Generic;
using System.Windows.Media;
using CrMap.Models;
namespace CrMap.ViewModels
{
public class CrMapViewModel
{
public IList<Symbol> Symbols { set; get; }
public int GridRows { set; get; }
public int GridCols { set; get; }
public CrMapViewModel()
{
fillDataSource();
}
private void fillDataSource()
{
Symbols = new List<Symbol>();
GridCols = 15;
var fontFamily = new FontFamily(new Uri("pack://application:,,,/"), "/Fonts/#whhglyphs");
GlyphTypeface glyph = null;
foreach (var typeface in fontFamily.GetTypefaces())
{
if (typeface.TryGetGlyphTypeface(out glyph) && (glyph != null))
break;
}
if (glyph == null)
throw new InvalidOperationException("Couldn't find a GlyphTypeface.");
GridRows = (glyph.CharacterToGlyphMap.Count / GridCols) + 1;
foreach (var item in glyph.CharacterToGlyphMap)
{
var index = item.Key;
Symbols.Add(new Symbol
{
Character = Convert.ToChar(index),
CharacterCode = string.Format("&#x{0:X}", index)
});
}
}
}
}<Window x:Class="CrMap.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:CrMap.ViewModels"
Title="MainWindow" WindowStartupLocation="CenterScreen" WindowState="Maximized"
Height="350" Width="525">
<Window.Resources>
<vm:CrMapViewModel x:Key="vmCrMapViewModel" />
</Window.Resources>
<ScrollViewer VerticalScrollBarVisibility="Visible">
<ItemsControl
DataContext="{StaticResource vmCrMapViewModel}"
ItemsSource="{Binding Symbols}"
Name="MainItemsControl"
VerticalAlignment="Top"
HorizontalAlignment="Center"
Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="4">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid
HorizontalAlignment="Center"
VerticalAlignment="Center"
Columns="{Binding GridCols}"
Rows="{Binding GridRows}">
</UniformGrid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl>
<Border BorderBrush="SlateGray"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
BorderThickness="1" CornerRadius="3" Margin="3">
<StackPanel Margin="3" Orientation="Vertical">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="Fonts/#whhglyphs"
Foreground="DarkRed"
FontSize="17"
Text="{Binding Character}" />
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{Binding CharacterCode}" />
</StackPanel>
</Border>
</ContentControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Window>