Mittwoch, 25. Dezember 2013

Optimizing the implementation of INotifyPropertyChanged using [CallerMemberName] in .Net 4.5 and above

Heard about it first time at the BASTA! in Darmstadt, Feb. 2013:
M.Steyer showed new options/freatures of the recent .Net Version. There was also an article in the Windows.developer Magazine by G.Biswanger about it, later. I will pick it up here, too:

The View:

<Window x:Class="WpfApplication1_BindBaseDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:project="clr-namespace:WpfApplication1_BindBaseDemo"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<project:MainWindowViewModel></project:MainWindowViewModel>
</Window.DataContext>
<Grid>
<TextBlock Text="{Binding MyText}"></TextBlock>
</Grid>
</Window>

The ViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApplication1_BindBaseDemo
{
  public class MainWindowViewModel : BindBase
  {
    private string myText;
    public string MyText
    {
      get { return myText; }
      set { SetProperty(ref myText,value); }
    }
    public MainWindowViewModel()
    {
      MyText = "Hello World";
    }
  }
}


The Class BindBase.cs using the new [CallerMemberName]:

using System;
using System.Collections.Generic;
using System.ComponentModel; // for INotifyPropertyChanged
using System.Linq;
using System.Runtime.CompilerServices; // for CallerMemberName
using System.Text;
using System.Threading.Tasks;
namespace WpfApplication1_BindBaseDemo
{
  public abstract class BindBase : INotifyPropertyChanged
  {
    public event PropertyChangedEventHandler PropertyChanged;
    protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName]String propertyName = null)
    {
      if (object.Equals(storage, value)) return false;
      storage = value;
      this.OnPropertyChanged(propertyName);
      return true;
    }
    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
      var eventHandler = this.PropertyChanged;
      if (eventHandler != null)
        eventHandler(this, new PropertyChangedEventArgs(propertyName));
    }
  }
}
Benefit from the best Windows Desktop app in the world and use Strokey.Net!

Montag, 23. Dezember 2013

Schema of a WPF TrayIcon Application

Note: only an overview! (You should add a context menu by clicking on the trayIcon instead showing an BalloonTip and respond on its closing.)

a) create a new wpf application project
b) add an Icon to your Project and let the resource copy to your bin folder
c) add reference to System.Windows.Forms - it Hosts the NotifyIcon!
d) add System.Drawing.Imaging - used to load the *.ico file in the "bin" Folder
e) Name the MainWindows "mainWin" in XAML.
f) add the Events "Closing" and "Closed" to the XAML of the MainWindow!
g) consider the following sample:



Benefit from the best Windows Desktop app in the world and use Strokey.Net!


 

Donnerstag, 22. August 2013

wpf mvvm? drag and drop? easy? fast result?

This is not a 100% wpf mvvm approach, because this solution will use the help of the default code behind file. As we know, we should avoid touching the code behind file in case of a clean mvvm. Calling a ViewModel-method through the drop event of the default code behind file....

in the code behind file:

using MyProject.MyViewModels;

...
private void UserControl_Drop(object sender, DragEventArgs e)
{
      (this.DataContext as UserControlVieModel).OnDrop(sender, e);
}


in the ViewModel:

public void OnDrop(object sender, System.Windows.DragEventArgs e)
{
            ExampleText= "dropped";
} 

Benefit from the best Windows Desktop app in the world and use Strokey.Net!

how to update the UI from another thread / parallel task

Use MYCLASS.Dispatcher.Invoke and post code for the queue of the UI thread.
Imagine, you will fill a listbox in an async & await initialized parallel thread or in another object.

...
MyMainWindowInstance.Dispatcher.Invoke(DispatcherPriority.Normal, (Actiondelegate() { MyMainWindowInstance.MyListBox.Items.Add("from parallel!!!!!!!!"); }  );
...

Benefit from the best Windows Desktop app in the world and use Strokey.Net!

Dienstag, 13. August 2013

How to bind to a static resource of - for instance - App.xaml.cs class?

1) define the appropriate Namespace of your Project in XAML...

                        xmlns:Project="clr-namespace:MyCurrentProject"


2) bind a property (IsEnabled)  to a boolean static source in XAML (for instance isRegistered true/false) ...

           ...             IsEnabled="{Binding Source={x:Static Project:App.isRegistered}}"  ...

Benefit from the best Windows Desktop app in the world and use Strokey.Net!

How to show a tooltip only if the element/target is disabled/is not enabled ...

<button click="myButton_Click_1" content="Testtext" isEnabled="{Binding myButtonIsEnabledState}" name="myButton"
ToolTip="{Binding disabledStateToolTipText}" ToolTipService.isEnabled="{Binding Source={x:Static ProjectNamespace:App.isNotRegistered}}" ToolTipService.showOnDisabled="True"> </button>
The Default behavior is to Switch off the ToolTip via ToolTipService.isEnabled +++ The ToolTip will only be active, if the Button is not enabled using ToolTipService.IsEnabled property, because ToolTipService.showOnDisabled="True" overrides the previous "false"-ToolTip functionality.

myButtonIsEnabledState = true or false ViewModel Member for basic button on/off  

disabledStateToolTipText = text to show as a tooltip

App.isNotRegistered = here global static true/false-Switch to modify the tooltip visibility

Benefit from the best Windows Desktop app in the world and use Strokey.Net!