Mittwoch, 24. Februar 2016

Static Binding to Object of a DataClass

Central Object of Data Class is located in App.xaml.cs for instance...

 private static IniStructure _iniObj;
        public static IniStructure IniObj
        {
            get { return _iniObj; }
            set { _iniObj = value; }
        }

This IniObj contains a member called "Username".

add the application namespace to xaml

xmlns:myProj="clr-namespace:MyProjectNamespace"

add the binding to the property Text of a TextBox

Text="{Binding Source={x:Static myProj:App.IniObj}, Path=Username}"


Dienstag, 7. Januar 2014

Localization, ResourceManager, Dictionary, XAML Binding (VS12, .Net4.5)

Goal: easy implemetation of localization in the UI. Show a localized label... (VS/.Net, C#, WPF)

Requirement for all variants:

1) The base - were the words are .... in the VS Project "Properties" Section imagine a Resourcefile like "tx.resx" for english default keys&values and a Resourcefile "tx.de.resx" for the equivalent german keys&values. For instance there is a key called MyKeyToShowATranslatedValue. This key points to "Hello world" in the tx.resx and to "Hallo Welt" in the tx.de.resx.



Sample A:

2) In the View (XAML)...

Note: To use this variant, you need to doubleclick/open the Resourcefile tx.resx and tx.de.resx in VS. There, you see a combobox to change the "Access modifier". Change it to "public" and save each tx* file.

xmlns:propx="clr-namespace:PROJECTNAMESPACE.Properties"
           Text="{x:Static propx:tx.MyKeyToShowATranslatedValue}"/>

(Ready)




Sample B:



2) add in "App.xaml.cs" and initialize the "tx" ResourceManager

public static ResourceManager tx = new ResourceManager("PROJECTNAMESPACE.Properties.tx", System.Reflection.Assembly.GetExecutingAssembly());

3) In the ViewModel-class...

3.1) create a Member to store the translated Words of tx.resx or tx.de.resx, whatever...

        private Dictionary<string, string> _words = new Dictionary<string,string>();
        public Dictionary<string, string> Words
        {
            get { return _words; }
        }
3.2) in the constructor of the ViewModel-class, copy the values of the ResourceManager in the Dictionary...

ResourceSet rs = App.tx.GetResourceSet(Thread.CurrentThread.CurrentUICulture, true, true);
            foreach (DictionaryEntry de in rs)
            {
                _words.Add(de.Key.ToString(), de.Value.ToString());
            }

4) In the XAML-View...

4.1) set the DataContext to the appropriate ViewModel

4.2) use the values of the Dictionary Keys in the VIEW using {Binding}....

Text="{Binding Path=Words[MyKeyToShowATranslatedValue]}" 


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

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!