Search This Blog

Monday, May 19, 2014

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;
}

Friday, November 29, 2013

WPF Resources - Static and Dynamic example

WPF > Resources

 
A resource is an object. This object is reused in different places in an application.
  • XAML resources (brushes, styles)
  • data files
StaticResource 
It is assigned to the property during the loading of the XAML and no need for application to run. It is assigned only once.

DynamicResource
It is lookup for the resource in runtime.

Example

You see in this image static resource is loaded during design XAML:

 
XAML:


<Window x:Class="WpfApplication4.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">
    <Window.Resources>
        <SolidColorBrush x:Key="brush" Color="Red" />
    </Window.Resources>
    <Grid>
        <Grid.Resources>
            <SolidColorBrush x:Key="bgcolor" Color="Green"/>
        </Grid.Resources>

        <TextBlock x:Name="txt" Background="{StaticResource bgcolor}"  Margin="113,92,145,129">StaticResource</TextBlock>

        <Button x:Name="btn" Content="Dynamic Resource" HorizontalAlignment="Left" VerticalAlignment="Top" Width="114" Click="Button_Click"/>
    </Grid>
</Window>

C# Code:

   this.btn.SetResourceReference(BackgroundProperty, "brush");
Dynamic resource is loaded in runtime