Skip to content

Instantly share code, notes, and snippets.

@JohanLarsson
Last active August 29, 2015 13:57
Show Gist options
  • Save JohanLarsson/9662344 to your computer and use it in GitHub Desktop.
Save JohanLarsson/9662344 to your computer and use it in GitHub Desktop.
using System;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
public class MainMenuButton : ButtonBase
{
public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register(
"ImageSource",
typeof(ImageSource),
typeof(MainMenuButton),
new PropertyMetadata(default(ImageSource)));
public static readonly DependencyProperty DisplayNameProperty = DependencyProperty.Register(
"DisplayName",
typeof(string),
typeof(MainMenuButton),
new PropertyMetadata(default(string)));
public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register(
"Description",
typeof(string),
typeof(MainMenuButton),
new PropertyMetadata(default(string)));
public static readonly DependencyProperty UriProperty = DependencyProperty.Register(
"Uri",
typeof(Uri),
typeof(MainMenuButton),
new PropertyMetadata(default(Uri), OnUriChanged));
public ImageSource ImageSource
{
get
{
return (ImageSource)GetValue(ImageSourceProperty);
}
set
{
SetValue(ImageSourceProperty, value);
}
}
public string DisplayName
{
get
{
return (string)GetValue(DisplayNameProperty);
}
set
{
SetValue(DisplayNameProperty, value);
}
}
public string Description
{
get
{
return (string)GetValue(DescriptionProperty);
}
set
{
SetValue(DescriptionProperty, value);
}
}
public Uri Uri
{
get
{
return (Uri)GetValue(UriProperty);
}
set
{
SetValue(UriProperty, value);
}
}
private static void OnUriChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var mainMenuButton = (MainMenuButton)o;
if (mainMenuButton == null)
{
return;
}
mainMenuButton.CommandParameter = e.NewValue;
}
}
using System;
using System.Linq;
using System.Linq.Expressions;
/// <summary>
/// Class provides methods to obtain member names of data types.
/// </summary>
public static class NameOf
{
/// <summary>
/// Returns the name of a property provided as a property expression.
/// </summary>
/// <typeparam name="T">Type of the property.</typeparam>
/// <param name="propertyExpression">Property expression on the the form () => Instance.Property.</param>
/// <returns>Returns the simple name of the property.</returns>
public static string Property<T>(Expression<Func<T>> propertyExpression)
{
var path = PathExpressionVisitor.GetPath(propertyExpression);
if (path.Length == 1)
{
return path.Single().Member.Name;
}
throw new Exception("Trying to get the name of a nested property: " + string.Join(".", path.Select(x => x.Member.Name)));
}
}
using System;
using Common.Helpers;
using NUnit.Framework;
public class NameOfTests
{
public string DummyProperty { get; private set; }
[Test]
public void NameOfPropertyHappyPath()
{
var name = NameOf.Property(() => DummyProperty);
Assert.AreEqual("DummyProperty", name);
}
[Test]
public void ThrowsOnNestedProperty()
{
var exception = Assert.Throws<Exception>(() => NameOf.Property(() => DummyProperty.Length));
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
public class PathExpressionVisitor : ExpressionVisitor
{
private readonly List<MemberExpression> _path = new List<MemberExpression>();
public static MemberExpression[] GetPath<TSource, TResult>(Expression<Func<TSource, TResult>> expression)
{
var visitor = new PathExpressionVisitor();
visitor.Visit(expression.Body);
return Enumerable.Reverse(visitor._path).ToArray();
}
public static MemberExpression[] GetPath<T>(Expression<Func<T>> expression)
{
var visitor = new PathExpressionVisitor();
visitor.Visit(expression.Body);
return Enumerable.Reverse(visitor._path).ToArray();
}
protected override Expression VisitMember(MemberExpression node)
{
_path.Add(node);
return base.VisitMember(node);
}
}
<WrapPanel Grid.Column="1" VerticalAlignment="Center">
<WrapPanel.Resources>
<ControlTemplate x:Key="MainMenuButtonTemplate" TargetType="{x:Type controls:MainMenuButton}">
<Grid HorizontalAlignment="Left" Width="250" Height="250" Margin="3">
<Grid.RowDefinitions>
<RowDefinition Height="200" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<Border Background="{StaticResource ItemPlaceholderBackgroundThemeBrush}" Name="Border"
BorderThickness="4"
BorderBrush="{StaticResource ItemPlaceholderBackgroundThemeBrush}">
<Image Source="{TemplateBinding ImageSource}" Stretch="None" />
</Border>
<StackPanel Margin="0,0,0,0" Grid.Row="1" MinHeight="50" Name="Panel"
VerticalAlignment="Bottom"
Background="{StaticResource ItemOverlayBackgroundThemeBrush}">
<TextBlock Text="{TemplateBinding DisplayName}"
Foreground="White"
FontSize="16"
Margin="15,5,15,0" />
<TextBlock Text="{TemplateBinding Description}"
Foreground="Gray"
TextWrapping="NoWrap"
Margin="15,3,15,1" />
</StackPanel>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Border"
Property="BorderBrush"
Value="{DynamicResource Accent}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</WrapPanel.Resources>
<controls:MainMenuButton Template="{StaticResource MainMenuButtonTemplate}"
DisplayName="{Binding OrderNumber}"
ImageSource="{Binding HasOrder, Converter={StaticResource OrderConverter}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
Command="{Binding MenuCommand}"
Uri="/Map.Client;component/Pages/Order.xaml" />
</WrapPanel>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment