Search This Blog

Tuesday, April 8, 2014

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

 

Thursday, November 21, 2013

Example: Load and save content of WPF RichTextBox to a file

WPF > Controls  > ContentControl > RichTextBox

RichTextBox operates on FlowDocument objects.

Note: Pasting HTML content into a RichTextBox might result in unexpected behavior because RichTextBox uses RTF format rather than directly using HTML format.

Examples


Example

Load and save content of WPF RichTextBox to a file


XAML:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:src="clr-namespace:WpfApplication2"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfApplication2.MainWindow"
        Title="MainWindow"
       d:DesignWidth="300" d:DesignHeight="320"
        >
    <Grid HorizontalAlignment="Left" Height="295" VerticalAlignment="Top" Width="300">
        <Button x:Name="btnSave" Content="Save" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="btnSave_Click"/>
        <Button x:Name="btnLoad" Content="Load" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="80,0,0,0" Click="btnLoad_Click"/>
        <RichTextBox x:Name="rtb" HorizontalAlignment="Left" Height="267" Margin="0,27,0,0" VerticalAlignment="Top" Width="300">
            <FlowDocument>
                <Paragraph>
                    <Run Text=""/>
                </Paragraph>
            </FlowDocument>
        </RichTextBox>
    </Grid>
</Window>

C# Code

using System;
using System.Collections.Generic;
using System.Globalization;
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.Shapes;

namespace WpfApplication2
{
     /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    ///

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            TextRange t = new TextRange(rtb.Document.ContentStart,rtb.Document.ContentEnd);
            FileStream file = new FileStream("c:\\rtb.xaml", FileMode.Create);
            t.Save(file, System.Windows.DataFormats.XamlPackage);
             file.Close();
        }
        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            TextRange t = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
            FileStream file = new FileStream("c:\\rtb.xaml", FileMode.Open);
            t.Load(file, System.Windows.DataFormats.XamlPackage);
            file.Close();
        }
    }
}