Вот что я наконец получил:
<Window x:Class="ScratchClient.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="655.85" Width="687"
xmlns:local="clr-namespace:ScratchClient">
<Window.Resources>
<local:BarcodeConverter x:Key="barcodeConverter"/>
<ItemsPanelTemplate x:Key="HorizontalPanel">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
<DataTemplate x:Key="SingleBarTemplate">
<Rectangle Fill="{Binding color}" Width="{Binding width}" />
</DataTemplate>
<DataTemplate x:Key="SingleCodeTemplate">
<DockPanel>
<TextBlock DockPanel.Dock="Bottom" Text="{Binding}" />
<ItemsControl ItemsSource="{Binding Converter={StaticResource barcodeConverter}}"
ItemsPanel="{StaticResource HorizontalPanel}"
ItemTemplate="{StaticResource SingleBarTemplate}" />
</DockPanel>
</DataTemplate>
<DataTemplate x:Key="BarcodeTemplate">
<ItemsControl ItemsSource="{Binding}"
ItemsPanel="{StaticResource HorizontalPanel}"
ItemTemplate="{StaticResource SingleCodeTemplate}"
Height="100" />
</DataTemplate>
</Window.Resources>
<Grid>
<ContentPresenter Name="barcode" Content="*WIKIPEDIA*" ContentTemplate="{StaticResource BarcodeTemplate}">
<ContentPresenter.RenderTransform>
<ScaleTransform ScaleX="2" ScaleY="2"></ScaleTransform>
</ContentPresenter.RenderTransform>
</ContentPresenter>
</Grid>
public class BarcodeConverter : IValueConverter
{
//W Wide - Black
//N Narrow - Black
//w Wide - White
//n Narrow - White
#region code details
Dictionary<char, string> _codes = new Dictionary<char, string>
{
{'0',"NnNwWnWnN"},
{'1',"WnNwNnNnW"},
{'2',"NnWwNnNnW"},
{'3',"WnWwNnNnN"},
{'4',"NnNwWnNnW"},
{'5',"WnNwWnNnN"},
{'6',"NnWwWnNnN"},
{'7',"NnNwNnWnW"},
{'8',"WnNwNnWnN"},
{'9',"NnWwNnWnN"},
{'A',"WnNnNwNnW"},
{'B',"NnWnNwNnW"},
{'C',"WnWnNwNnN"},
{'D',"NnNnWwNnW"},
{'E',"WnNnWwNnN"},
{'F',"NnWnWwNnN"},
{'G',"NnNnNwWnW"},
{'H',"WnNnNwWnN"},
{'I',"NnWnNwWnN"},
{'J',"NnNnWwWnN"},
{'K',"WnNnNnNwW"},
{'L',"NnWnNnNwW"},
{'M',"WnWnNnNwN"},
{'N',"NnNnWnNwW"},
{'O',"WnNnWnNwN"},
{'P',"NnWnWnNwN"},
{'Q',"NnNnNnWwW"},
{'R',"WnNnNnWwN"},
{'S',"NnWnNnWwN"},
{'T',"NnNnWnWwN"},
{'U',"WwNnNnNnW"},
{'V',"NwWnNnNnW"},
{'W',"WwWnNnNnN"},
{'X',"NwNnWnNnW"},
{'Y',"WwNnWnNnN"},
{'Z',"NwWnWnNnN"},
{'-',"NwNnNnWnW"},
{'.',"WwNnNnWnN"},
{' ',"NwWnNnWnN"},
{'$',"NwNwNwNnN"},
{'/',"NwNwNnNwN"},
{'+',"NwNnNwNwN"},
{'%',"NnNwNwNwN"},
{'*',"NwNnWnWnN"},
};
#endregion
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string code;
int narrow = 1, wide = 3;
if (!_codes.TryGetValue((char)value, out code)) return null;
code += 'n';
var result = from i in Enumerable.Range(0, code.Length)
select new
{
color = (code[i] == 'W' || code[i] == 'N') ? Brushes.Black : Brushes.Transparent,
width = (code[i] == 'n' || code[i] == 'N') ? narrow : wide
};
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
И я создал элемент управления с открытым исходным кодом в github: https://github.com/KevinPan/wpf-barcode,, пожалуйста, попробуйте.
Надеюсь, это поможет.