files

Code Snippets

Title: Lambda Command
Description: A command similar to the Delegate command
Category: WPF
Visibility:  Only me
Added by: db7uk on 6/19/2012

 This snippet currently has no votes. Why not log in and be the first to rate it?

Get link for this code snippet
Collapse code snippet
Expand code snippet
Tweet this snippet
Tweet about this snippet
Delete code snippet
Edit code snippet
Imports System.Windows.Input

Namespace WPF.Commands
    ''' <summary>
    ''' Command for executing lambda expressions. Similar to the delegate command.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class LambdaCommand
        Implements ICommand

        Public Property CanExecuteLambda As Func(Of Object, Boolean)
        Public Property ExecuteLambda As Action(Of Object)

        Public Property Caption As String

        ''' <summary>
        ''' Initializes a new instance of the <see cref="LambdaCommand" /> class.
        ''' </summary>
        ''' <param name="caption">The caption.</param>
        ''' <param name="canExecuteLambda">The can execute lambda.</param>
        ''' <param name="executeLambda">The execute lambda.</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal caption As String, ByVal canExecuteLambda As Func(Of Object, Boolean), ByVal executeLambda As Action(Of Object))
            Me.Caption = caption
            Me.CanExecuteLambda = canExecuteLambda
            Me.ExecuteLambda = executeLambda
        End Sub

        ''' <summary>
        ''' Defines the method that determines whether the command can execute in its current state.
        ''' </summary>
        ''' <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        ''' <returns>true if this command can be executed; otherwise, false.</returns>
        ''' <remarks></remarks>
        Public Function CanExecute(ByVal parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
            Return Me.CanExecuteLambda.Invoke(parameter)
        End Function

        ''' <summary>
        ''' Occurs when changes occur that affect whether or not the command should execute.
        ''' </summary>
        ''' <remarks></remarks>
        Public Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements System.Windows.Input.ICommand.CanExecuteChanged

        ''' <summary>
        ''' Defines the method to be called when the command is invoked.
        ''' </summary>
        ''' <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        ''' <remarks></remarks>
        Public Sub Execute(ByVal parameter As Object) Implements System.Windows.Input.ICommand.Execute
            Me.ExecuteLambda.Invoke(parameter)
        End Sub

        ''' <summary>
        ''' Called when [can execute changed].
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub OnCanExecuteChanged()
            RaiseEvent CanExecuteChanged(Me, EventArgs.Empty)
        End Sub
    End Class
End Namespace

Title: Update textbox binding from IsDefault button press
Description: This attached behaviour triggers the update of the binding source when the user trggers an IsDefault button press without losing focus.
Category: WPF
Visibility:  Public
Added by: ohanlonp on 7/5/2012

 Currently rated 5 by 1 user(s)

Get link for this code snippet
Collapse code snippet
Expand code snippet
Tweet this snippet
Tweet about this snippet
Delete code snippet
Edit code snippet
using System.Windows.Interactivity;
    using System.Windows.Controls;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Data;
    /// <summary>
    /// Associate this behaviour with the button that you mark as IsDefault
    /// to trigger the ViewModel update when the user clicks enter in a textbox
    /// and the property doesn't update because the update source is set to
    /// lost focus.
    /// </summary>
    public class DefaultButtonUpdateTextBoxBindingBehavior : Behavior<Button>
    {
        /// <summary>
        /// Hook into the button click event.
        /// </summary>
        protected override void OnAttached()
        {
            AssociatedObject.Click += AssociatedObject_Click;
            base.OnAttached();
        }

        /// <summary>
        /// Unhook the button click event.
        /// </summary>
        protected override void OnDetaching()
        {
            AssociatedObject.Click -= AssociatedObject_Click;
        }

        /// <summary>
        /// The click event handler.
        /// </summary>
        void AssociatedObject_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // Get the element with the keyboard focus
            FrameworkElement el = Keyboard.FocusedElement as FrameworkElement;
            if (el != null && el is TextBox)
            {
                // Get the binding expression associated with the text property
                // for this element.
                BindingExpression expression = el.GetBindingExpression(TextBox.TextProperty);
                if (expression != null)
                {
                    // Now, trigger the update.
                    expression.UpdateSource();
                }
            }
        }
    }