Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Monday, November 15, 2010

Implementation of the ICommand interface for reuse.

    If you use MVVM (model-view-viewmodel) pattern, the most commonly used mechanism is the binding to action in the View using commands. To assignment the command needed to implement an interface ICommand. It's simple, but it will need to do again and again.

    The idea - creating a universal command that takes two delegates: one is called when ICommand.Execute (object param) invoked, the second checks the status of whether you can execute the command (ICommand.CanExecute (object param)).

    Requires the method to switching event CanExecuteChanged. It is called from the user interface elements for switching the state CanExecute () command.

public class ModelCommand : ICommand
{
    #region Constructors

    public ModelCommand(Action<object> execute)
        : this(execute, null) { }

    public ModelCommand(Action<object> execute, Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    #endregion

    #region ICommand Members

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        return _canExecute != null ? _canExecute(parameter) : true;
    }

    public void Execute(object parameter)
    {
        if (_execute != null)
            _execute(parameter);
    }

    public void OnCanExecuteChanged()
    {
        CanExecuteChanged(this, EventArgs.Empty);
    }

    #endregion

    private readonly Action<object> _execute = null;
    private readonly Predicate<object> _canExecute = null;
}

Wednesday, August 4, 2010

Microsoft Ribbon for WPF

I am pleased to inform you of the final release of Microsoft Ribbon for WPF. You can download the installer for this release here.


This is great news for Windows developers looking to build rich, fluent user experiences. From the MSDN documentation:
The ribbon is a command bar that organizes the features of an application into a series of tabs at the top of the application window. The ribbon user interface (UI) increases discoverability of features and functions, enables quicker learning of the application, and makes users feel more in control of their experience with the application. The ribbon replaces the traditional menu bar and toolbars.
In short, this control library gives you the ability to incorporate a ribbon user experience in your applications today.


For Windows developers, the ribbon provides a rich experience that you can incorporate quickly and easily for your WPF applications. What’s particularly cool about the Microsoft Ribbon for WPF is that it’s a control library that’s built 100% in pure WPF goodness.




File → New Project...
After downloading and installing the Microsoft Ribbon for WPF, you’ll find the WPF Ribbon Application project template listed for both Visual C# and Visual Basic:



This project template provides you with a very simple example to help get you started:




For a deeper examination of the XAML and control library that’s used to build this application, I strongly recommend that you read Pete Brown’s excellent summary of this release in a post entitled,Announcing: Microsoft Ribbon for WPF. Pete also provides an important point (with emphasis added):
The new ribbon control is compatible with WPF 3.5sp1 and WPF 4. This is a 100% WPF implementation, not a wrapper around native code. That means you get all the great WPF styling capabilities for the new control.
This fact cannot be underscored enough. A true WPF implementation means that you can incorporate styling and rely on assumed WPF functionality (i.e. RibbonButton support the ICommand interface – wahoo!). Having built my fair share of WPF applications in the past – in some cases, wrestling with control interop and styling issues – I love this news.
So, what are you waiting for? Download Microsoft Ribbon for WPF today and start incorporating it into your applications!
Related Links
PS: If you building applications with Windows Forms, check out Windows Ribbon for WinForms project on CodePlex.

Saturday, July 24, 2010

Debugging WPF Binding

  WPF implements the mapping of data models using {Binding} expression. However in some cases there may be difficulties with understanding that the value was received as a result of binding. In this article I want to talk about a possible decision to enable the debug mode to see the used values. The solution is not new and probably many about it know, but for those who started working with WPF it might be useful. The essence of solution lies in the fact that the binding is carried out not directly but through a simple converter that does nothing to transform, but allows you to set Breakpoint while debugging and see the value of object that used.
using System;
using System.Windows.Data;

namespace WpfApplication
{
  public class EmptyConverter : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      return value;
    }
  }
}

  This is how data binding is implemented to the element. It creates an object converter and used in the {Binding} expression.
<Window x:Class="WpfApplication.TestWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication"
    x:Name="Window">
 
  <Window.Resources>
    <local:EmptyConverter x:Key="EmptyConverter"/>
  </Window.Resources>
 
  <Label Content="{Binding Path=TestField, ElementName=Window, Converter={StaticResource EmptyConverter}}" /> 
</Window>


Tuesday, July 13, 2010

NullToBoolValueConverter for WPF

  You can enable or disable WPF elements based on if they are null or not. This is not supported, but you can create a simple value converter.
  /// <summary>
  /// Converts null to boolean.
  /// </summary>
  /// <remarks>
  /// If parameter is not specified or specified with null:
  /// if passed value is null then true is returned, otherwise false.
  /// If parameter is specified then inversion is done:
  /// if passed value is null then false is returned, otherwise true.
  /// </remarks>
  [ValueConversion(typeof(object), typeof(bool))]
  public class NullToBoolValueConverter : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      bool result = value == null ? true : false;
      if (parameter != null)
        return !result;
      return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      return value;
    }
  }


  This value converter allows me to bind to any property taking a boolean argument with the following syntax in XAML.
<local:NullToBoolValueConverter x:Key="NullToBoolValueConverter"/>

<ComboBox.IsEnabled>
  <Binding ElementName="TetsComboBox" Path="SelectedItem" Converter="{StaticResource NullToBoolValueConverter}" ConverterParameter="true" />
</ComboBox.IsEnabled>

Friday, April 2, 2010

WPF show or hide element based on CheckBox

  Today I had to do the task for showing or hiding elements based the value of a checkbox. In WinForms 2.0 for this purpose it was necessary to subscribe to event CheckedChanged and in his handler write the code:

private void myCheckBox_CheckedChanged(object sender, EventArgs e)
{
  myTextBox.Visible = myCheckBox.Checked;
}

  With the new Visibility Enum in WPF, this becomes a bit trickier. To accomplish this, you can use converter BooleanToVisibilityConverter that accept a boolean value and return a visibility value. Now we can cleanly show or hide an element based on a checkbox using just XAML code. The XAML code to use this is below:

<Window
  x:Class="MyTest.XamlOnly"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="XamlOnly" Height="300" Width="300">

  <Window.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
  </Window.Resources>

  <StackPanel Orientation="Vertical">
    <CheckBox Content="Check to show text box below me" Name="checkViewTextBox"/>
    <TextBox Text="only seen when above checkbox is checked" Visibility="{Binding Path=IsChecked, ElementName=checkViewTextBox, Converter={StaticResource BoolToVisConverter}}"/>
  </StackPanel>

</Window>