Code Stash

Squirrel Nuts
Jonathan Purdom
03 Mar 2016

Attached Properties

This Class uses a Behaviour to wire up the commands in the View Model to the View

View (.xaml)

xmlns:attProp="clr-namespace:Project"
attProp:AttachedProperties.RegisterCommandBindings="{Binding CommandBindings}"

View Model (.cs)

private readonly RoutedCommand _deleteDetailsCommand = new RoutedCommand();
private CommandBindingCollection _commandBindings;
public RoutedCommand DeleteDetailsCommand { get { return _deleteDetailsCommand; } }
public CommandBindingCollection CommandBindings { get { return _commandBindings; } }

private void InitializeCommandBindings()
{
_commandBindings = new CommandBindingCollection();
var deleteDetailsBinding = new CommandBinding(DeleteDetailsCommand, DeleteDetailsExecuted, (s, e) => e.CanExecute = true);
_commandBindings.AddRange(new[] { deleteDetailsBinding}
}

Numeric Input Behaviour

This Behaviour allows you to restrict data entry to numeric values only in the View

View (.xaml)

xmlns:behaviours="clr-namespace:Project.Behaviours"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

<TextBox Style="{StaticResource TextBoxSmall}" Text ="{Binding Txt}">
<i:Interaction.Behaviors>
<behaviours:NumericInputBehavior EnableDecimalInput="True" />
</i:Interaction.Behaviors>
</TextBox>

Password Box Assistant

This Is the View Code for implementing a password box in WPF

View (.xaml)

xmlns:attachingProperties="clr-namespace:Project.ViewModel.AttachingProperties"

<PasswordBox DockPanel.Dock="Right" attachingProperties:PasswordBoxAssistant.BindPassword="True" attachingProperties:PasswordBoxAssistant.BoundPassword="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource PasswordBoxStyle}" />