Search This Blog

Monday, May 19, 2014

WPF Converter Example

WPF > Data > Converters

Converters changes data from one type to another and apply custom logic to binding.
Implementation : create a class that implements the IvalueConverter.

Example:


XAML:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:YesNoConverter x:Key="YesNoConverter" />
    </Window.Resources>
    <StackPanel Margin="5">
        <CheckBox x:Name="chk" IsChecked="{Binding ElementName=txtYesNo, Path=Text, Converter={StaticResource YesNoConverter}}" Content="Yes/No" />
        <TextBox Name="txtYesNo" />
    </StackPanel>
</Window>

C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    ///
    public class YesNoConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            switch (value.ToString())
            {
                case "Yes":
                    return true;
                case "No":
                    return false;
            }
            return false;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool)
            {
                if ((bool)value == true)
                    return "Yes";
                else
                    return "No";
            }
            return "No";
        }
    }
    public partial class MainWindow : Window
    {
     
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}



Binding path to current source WPF

WPF > Binding > Path

What is Binding Path=. ?

Answer:

A period (.) path can be used to bind to the current source.

Tuesday, April 8, 2014

Expander Control WPF Example

WPF > Controls > Layout > Expander

Expander displays a header that has a collapsible window that displays content when user clicks arrow button.



Example




XAML


<Window x:Class="ImageManager.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <Expander Header="Expander1" HorizontalAlignment="Left" Margin="59,94,0,0" VerticalAlignment="Top">
           <TextBlock Text="Expander 1" />
        </Expander>
        <Expander Header="Expander2" HorizontalAlignment="Left" Margin="59,141,0,0" VerticalAlignment="Top">
            <TextBlock Text="Expander 2" />
        </Expander>
    </Grid>

</Window>

ListBox Expander Binding WPF Example

WPF > Controls > Layout > Expander > In ListBox

ListBox Expander Binding WPF Example


XAML

<Window x:Class="ImageManager.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <ListBox Name="lst" Margin="5" ItemsSource="{Binding}" HorizontalContentAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Margin="5" FontSize="16" FontWeight="Bold" Text="{Binding Path=Name}"/>
                        <Expander Name="expender">
                            <Border Margin="5" Background="Wheat" BorderThickness="2" BorderBrush="#FF7A3E3E">
                                <StackPanel>
                                    <TextBlock Margin="5, 0"  Foreground="Blue" Text="{Binding Path=FreeSpace}"/>
                                    <TextBlock Margin="5, 0"  Foreground="Blue" Text="{Binding Path=Type}"/>
                                </StackPanel>
                            </Border>
                        </Expander>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

C# Code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ImageManager
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
   
    public class FileInfo
    {
        public FileInfo(string driveName, string freeSpace, string type)
         {
             Name = driveName;
             FreeSpace = "Free space: " +  freeSpace;
             Type = "Type: " + type;
         }
        public string Name
        { get; set; }
        public string FreeSpace
         { get; set; }
        public string Type
        { get; set; }
     
     }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<FileInfo> states = new List<FileInfo>();
            DriveInfo[] ListDrives = DriveInfo.GetDrives();

            foreach (DriveInfo Drive in ListDrives)
            {
              if(Drive.IsReady )
                    states.Add(new FileInfo(Drive.Name, Drive.AvailableFreeSpace.ToString(), Drive.DriveType.ToString()) );
                  
            }
            lst.ItemsSource = states;
        }


        void FileDirSearch(string sDir, List<string> sfiles, string pattern)
        {
            try
            {
                foreach (string d in Directory.GetDirectories(sDir))
                {
                    foreach (string f in Directory.GetFiles(d, pattern))
                    {
                        sfiles.Add(f);
                    }
                    FileDirSearch(d, sfiles, pattern);
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }
    }
}


Friday, January 24, 2014

Open File Dialog WPF Example

WPF > Windows > OpenFileDialog


Open File Dialog  WPF Example

Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document";
dlg.DefaultExt = ".doc";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
   string filename = dlg.FileName;
}