Created
November 9, 2011 22:20
-
-
Save robfe/1353306 to your computer and use it in GitHub Desktop.
WPF templated control for QrCode.codeplex.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Media; | |
using Gma.QrCodeNet.Encoding; | |
namespace Gma.QrCodeNet.Wpf | |
{ | |
public class QrCodeElement : Control | |
{ | |
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof (string), typeof (QrCodeElement), new UIPropertyMetadata("", (o, args) => ((QrCodeElement) o).UpdatePath())); | |
public static readonly DependencyProperty ErrorCorrectionProperty = DependencyProperty.Register("ErrorCorrection", typeof (ErrorCorrectionLevel), typeof (QrCodeElement), new UIPropertyMetadata(ErrorCorrectionLevel.M, (o, args) => ((QrCodeElement) o).UpdatePath())); | |
public static readonly DependencyPropertyKey PathGeometryProperty = DependencyProperty.RegisterReadOnly("PathGeometry", typeof (Geometry), typeof (QrCodeElement), new UIPropertyMetadata(new RectangleGeometry(new Rect(0, 0, 1, 1)))); | |
static QrCodeElement() | |
{ | |
DefaultStyleKeyProperty.OverrideMetadata(typeof (QrCodeElement), new FrameworkPropertyMetadata(typeof (QrCodeElement))); | |
BackgroundProperty.OverrideMetadata(typeof (QrCodeElement), new FrameworkPropertyMetadata(Brushes.White)); | |
ForegroundProperty.OverrideMetadata(typeof (QrCodeElement), new FrameworkPropertyMetadata(Brushes.Black)); | |
PaddingProperty.OverrideMetadata(typeof (QrCodeElement), new FrameworkPropertyMetadata(new Thickness(20))); | |
} | |
public QrCodeElement() | |
{ | |
UpdatePath(); | |
} | |
public Geometry PathGeometry | |
{ | |
get { return (Geometry) GetValue(PathGeometryProperty.DependencyProperty); } | |
private set { SetValue(PathGeometryProperty, value); } | |
} | |
[Category("Common Properties")] | |
public string Text | |
{ | |
get { return (string) GetValue(TextProperty); } | |
set { SetValue(TextProperty, value); } | |
} | |
[Category("Common Properties")] | |
public ErrorCorrectionLevel ErrorCorrection | |
{ | |
get { return (ErrorCorrectionLevel) GetValue(ErrorCorrectionProperty); } | |
set { SetValue(ErrorCorrectionProperty, value); } | |
} | |
void UpdatePath() | |
{ | |
PathGeometry = new GeometryGroup {Children = new GeometryCollection(GetFilledSquares())}; | |
} | |
IEnumerable<Geometry> GetFilledSquares() | |
{ | |
BitMatrix bitMatrix = new QrEncoder(ErrorCorrection).Encode(Text).Matrix; | |
for (int x = 0; x < bitMatrix.Width; x++) | |
{ | |
for (int y = 0; y < bitMatrix.Height; y++) | |
{ | |
if (bitMatrix[x, y]) | |
{ | |
yield return new RectangleGeometry(new Rect(x, y, 1, 1)); | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ResourceDictionary | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:Wpf="clr-namespace:Gma.QrCodeNet.Wpf"> | |
<Style TargetType="{x:Type Wpf:QrCodeElement}"> | |
<Setter Property="Template"> | |
<Setter.Value> | |
<ControlTemplate TargetType="{x:Type Wpf:QrCodeElement}"> | |
<Border Background="{TemplateBinding Background}" | |
BorderBrush="{TemplateBinding BorderBrush}" | |
BorderThickness="{TemplateBinding BorderThickness}"> | |
<Path Data="{TemplateBinding PathGeometry}" | |
Fill="{TemplateBinding Foreground}" | |
Stretch="Fill" | |
Margin="{TemplateBinding Padding}"/> | |
</Border> | |
</ControlTemplate> | |
</Setter.Value> | |
</Setter> | |
</Style> | |
</ResourceDictionary> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ResourceDictionary | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:Wpf="clr-namespace:Gma.QrCodeNet.Wpf"> | |
<Style TargetType="{x:Type Wpf:QrCodeElement}"> | |
<Setter Property="Template"> | |
<Setter.Value> | |
<ControlTemplate TargetType="{x:Type Wpf:QrCodeElement}"> | |
<Border Background="{TemplateBinding Background}" | |
BorderBrush="{TemplateBinding BorderBrush}" | |
BorderThickness="{TemplateBinding BorderThickness}"> | |
<Path Data="{TemplateBinding PathGeometry}" | |
Fill="{TemplateBinding Foreground}" | |
Stretch="Fill" | |
Margin="{TemplateBinding Padding}"/> | |
</Border> | |
</ControlTemplate> | |
</Setter.Value> | |
</Setter> | |
</Style> | |
</ResourceDictionary> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment