Search This Blog

Thursday, November 14, 2013

Routed event WPF

WPF > routed event

A routed event can invoke handlers on multiple listeners in an element tree.

Example

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <Border Height="50"  BorderBrush="White" BorderThickness="1">
            <StackPanel Orientation="Horizontal" Button.Click="CommonClickHandler">
                <Button Name="Button" Click="Button_Click">Button</Button>
                <CheckBox x:Name="CheckBox" />
            </StackPanel>
        </Border>
    </Grid>
</Window>

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.Shapes;

namespace WpfApplication1
{

    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void CommonClickHandler(object sender, RoutedEventArgs e)
        {
            FrameworkElement feSource = e.Source as FrameworkElement;
            switch (feSource.Name)
            {
                case "Button":
                    MessageBox.Show("Event fired on Button"); 
                    break;
                case "CheckBox":
                    MessageBox.Show("Event fired on CheckBox"); 
                    break;
            }
            e.Handled = true;
        }
    }
}

Wednesday, November 13, 2013

Master detail DataGrid example with code behind

WPF > ControlsItemControls > DataGridMaster detail 2 grids

In this example we will use two DataGrid controls to display master detail data from code behind using SelectionChanged event.


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:d="http://schemas.microsoft.com/expression/blend/2008"
        Title="MainWindow" Height="447.91" Width="514.871" Loaded="Window_Loaded">
    <Grid Margin="0,0,2,81">
        <DataGrid x:Name="dg" HorizontalAlignment="Left" VerticalAlignment="Top" Height="196" Width="507" SelectionChanged="dg_SelectionChanged"/>
        <DataGrid x:Name="dg1" HorizontalAlignment="Left" VerticalAlignment="Top" Height="214" Width="505" Margin="0,201,0,-78"/>
    </Grid>
</Window>

C# Code:



namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    ///
    public class Skill
    {
        public int Years { get; set; }
        public string Name { get; set; }
    }
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Position { get; set; }
        public List<Skill> SkillList { get; set; } 
    }
    public class Persons : ObservableCollection<Person>
    {
        public Persons()
        {
            Add(new Person() { Id = 1, Name = "Person1", Position = "Manager", SkillList = new List<Skill>() });
            Add(new Person() { Id = 2, Name = "Person2", Position = "Programmer", SkillList = new List<Skill>() });
            Add(new Person() { Id = 3, Name = "Person3", Position = "Programmer", SkillList = new List<Skill>() });
            Add(new Person() { Id = 4, Name = "Person4", Position = "Admin", SkillList = new List<Skill>() });
            Add(new Person() { Id = 5, Name = "Person5", Position = "Tester", SkillList = new List<Skill>() });
        }
    }
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
         {
            Persons pList = new Persons();
            pList[1].SkillList.Add(new Skill() {Years = 5, Name="C++"});
            pList[1].SkillList.Add(new Skill() {Years = 15, Name = "Visual Basic" });
            dg.ItemsSource = pList;
        }
        private void dg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            dg1.ItemsSource = ((Person)e.AddedItems[0]).SkillList;
        }
    }
}






DataGridRow WPF

WPF > DataGrid > DataGridRow

DataGridRow represents a single data item in a bound data source. Each row contains one cell per column and each cell displays a single value in the bound data item.

Changing DataGridRow Appearance on Mouse Events

WPF > DataGrid > DataGridRow > Style

Example: Change DataGridRow  appearance on Mouse Over



XAML:


<Window x:Class="WpfApplication2.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">
    <Window.Resources>
        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="LightCyan" />
                    <Setter Property="FontSize" Value="30" />
                    <Setter Property="Foreground"   Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <Grid>
        <DataGrid x:Name="dg" HorizontalAlignment="Left" VerticalAlignment="Top" Height="310" Width="507"/>
    </Grid>
</Window>
C# Code:

    public class Person
    {
        public string Name { get; set; }
        public string Position { get; set; }
    }

    public class Persons : ObservableCollection<Person>
    {
        public Persons()
        {
            Add( new Person() { Name ="Person1", Position = "Manager" } );
            Add(new Person() { Name = "Person2", Position = "Programmer" });
            Add(new Person() { Name = "Person3", Position = "Programmer" });
            Add(new Person() { Name = "Person4", Position = "Admin" });
            Add(new Person() { Name = "Person5", Position = "Tester" });
        }
    }

   dg.ItemsSource = new Persons();