Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

How to implement INotifyPropertyChanged interface in C# and WPF


 
To notify a target element in WPF that the source property value has been changed , The containing class will have to implement the INotifyPropertyChanged Interface 


Below is a sample abstract class that implements this interface, All you have to do is extend this abstract class in your ViewModel class or any class where your properties are declared.


 using System;
 using System.ComponentModel;

 namespace Logiphix
 {
    abstract public class PropogatePropertyChangesToTarget :  INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string info)
        {
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
        }
    }
}


How can you use it?

Lets declare a sample class and extend the above created abstract class

public class SampleClass PropogatePropertyChangesToTarget 
{
      /* I have my properties declared here*/
      private string employeeName;

      public string EmployeeName 
      {
          get
             { 
                   return employeeName;
             }

          set
             {
                   employeeName = value;

                   // Call the method here in the setter
                   NotifyPropertyChanged("EmployeeName"); 
             }
      }
}

Now if any target property is bound to this above property, It is likely to get notified that its value has changed.





How to check the Windows OS registry if a software is installed in the system using C#

Many a times it might be required that the Windows OS registry be checked to find out if a software is installed. we can do this very easily using C# as follows.

using System;
using System.Text;
using Microsoft.Win32;

namespace Logiphix
{
    class Program
    {
         static void Main(string[] args)
         {
                bool isInstalled = false;
                string registry_key = @"SOFTWARE\7-Zip";
                using (Microsoft.Win32.RegistryKey key = Registry.CurrentUser.OpenSubKey(registry_key))
                {
                      if (key != null)
                      {
                               isInstalled = true
                               /* This is the place where you may want to put the code if the software is installed
                                  i am just setting a flag here to be true.
                               */
                      }
                      else
                      {
                               isInstalled = false;
                      }
                }

                Console.WriteLine(isInstalled ? "The software is installed" : "The Software is not installed");
                Console.ReadKey();
         }
     }
}

This is a simple console app, Here i am trying to check if the 7-ZIP software is installed in my local machine. You can use the regedit to find out the registry key for that particular software on a machine where it is already installed and then use it in your code as above.

To use Regedit type regedit in the run window the following window will open




Copy the registry key as below.(as shown for 7-Zip , you can find the required software key by just navigating as below and then right click on the folder and select Copy Key Name


To use the copied key name:
For example if the current key name is as  for 7-Zip    HKEY_CURRENT_USER\Software\7-Zip
use only the string from       Software\       followed by the remaining string.
The    HKEY_CURRENT_USER\    can be discarded. 
Use a little of trial and error for your requirements

For example:   string registry_key = @"SOFTWARE\7-Zip";

This Should do the job for you.




WPF : TextBox

Text in WPF can be handled in. many ways, basically there are three types

  • The Simple text box or the plain text box
  • The Rich text box.
The two types mentioned above share the same base class the TextBoxBase.
The Simple text box or the plain text box allows you to enter only simple string. If you want to format your string. you have to use the  Rich text box. which allows you to have formatted text/ string. The Rich text box doesnot use the RTF / Rich text format even if the name suggests you so. it uses the WPF's own flow formatting to format the text.
Even though it supports RTF, it just doesn't use it as its internal model.
As they support a common base class they support the common clipboard operations like copying editing and pasting etc.

There is another type of text box that doesn't share the same base 

  • The Password Box
This is as the name suggests used for the password entering purpose . it doesn't share the same base class as the above two. because it is required that it shouldn't support some of the clip board operations like copy and paste. it also shows the entered text as dots/stars etc.

The TextBox and the RichTextBox both support spell checking. 
All we have to do is set the property SpellCheck.IsEnabled ="True"


<Page 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  
   <Grid>
<TextBox   SpellCheck.IsEnabled ="True" />

   </Grid>
</Page>

You can see below that the spell check works as expected, the misspelled words have been underlined with red. it can be right clicked to select from a list of words.

Again it depends on the localization. and provides the options depending on it.

The Example shown below is been developed on XamlPadX.
You can download this from http://blogs.msdn.com/b/llobo/archive/2006/12/30/xamlpadx-v2.aspx









Content Model : GroupBox Headers other than just a plain text in WPF

How to have a Group box in WPF / XAML with a header other than just plain text?

We know that  WPF / XAML supports Content Model that allows us to have elements arranged in a way that makes it possible to have anything to be used as a caption!
It could be just a plain text or some graphics or some data aswell.

For example,


<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >

  <Grid>
<GroupBox>
    <GroupBox.Header>
          <StackPanel Orientation="Horizontal">
                       <Button><TextBlock Text="Click Me!" /></Button>
          </StackPanel>
    </GroupBox.Header>
</GroupBox>
 </Grid>

</Page>

Here in the xaml above, the groupbox header is not just a plain text , it is a button with the text Click Me! in it, this is done within the <GroupBox.Header> </GroupBox.Header>