Search This Blog

Wednesday, March 2, 2016

Binding image in DataGrid WPF example

XAML

<Window x:Class="WpfApplication1.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>
        <DataGrid x:Name="dataGrid1" AutoGenerateColumns="False" SelectionChanged="dataGrid1_SelectionChanged">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding PictureName}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding PictureFilePath}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>

</Window>



C#

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace WpfApplication1
{
    public class PictureObject
    {
        public string PictureName { get; set; }
        public Uri PictureFilePath { get; set; }
    }
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<PictureObject> list = new List<PictureObject>();
            list.Add(new PictureObject() { PictureFilePath = new Uri("file:///c:\\Windows\\System32\\PerfCenterCpl.ico"), PictureName="Picture1" });
            dataGrid1.ItemsSource = list;
        }
    }
}

Friday, February 26, 2016

StatusBarItem Example WPF

WPF > Controls  > ContentControl > StatusBarItem

StatusBarItem objects are used to define items in a StatusBar control.





<Window x:Class="WpfApplication1.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">
    <Grid>
        <StatusBar Name="sbar"
           VerticalAlignment="Bottom" >
            <StatusBar.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black" Offset="0"/>
                    <GradientStop Color="#FFAE1B1B" Offset="1"/>
                </LinearGradientBrush>
            </StatusBar.Background>
            <StatusBarItem>
                <TextBlock Foreground="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}">Processsing</TextBlock>
            </StatusBarItem>
            <StatusBarItem>
                <ProgressBar Width="100" Height="20"
                 Name="progressBar1">
                    <ProgressBar.Triggers>
                        <EventTrigger RoutedEvent="ProgressBar.Loaded">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation
                Storyboard.TargetName="progressBar1"
                Storyboard.TargetProperty="Value"
                From="0" To="100" Duration="0:0:5"  />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </ProgressBar.Triggers>
                </ProgressBar>
            </StatusBarItem>
            <Separator/>
            <StatusBarItem>
                <TextBlock Foreground="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}">Percent</TextBlock>
            </StatusBarItem>
        </StatusBar>
    </Grid>

</Window>

Wednesday, January 27, 2016

ToogleButton WPF Example

WPF > Controls  > ContentControl > ToogleButton

Toggle button is similar to CheckBox control. It holds its state when it clicked.

Example 
On/Off Button






<Window x:Class="WpfApplication3.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>
        <Style x:Key="ToggleButtonStyle" TargetType="ToggleButton">
            <Setter Property="Content" Value="Off"/>
            <Setter Property="Foreground" Value="Red" />
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Content" Value="On">
                    </Setter>
                    <Setter Property="Foreground" Value="Green" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <ToggleButton Name="btnToogle" IsChecked="True"
                    Height="30" Width="70"
                      Style="{StaticResource ToggleButtonStyle}">
        </ToggleButton>
    </Grid>
</Window> 



Wednesday, January 6, 2016

ListBoxItem Example WPF

WPF > Controls  > ContentControl > ListBoxItem

ListBoxItem represents a selectable item in a ListBox.

Example




<Window x:Class="WpfApplication3.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">
    <Grid>
        <ListBox HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100">
            <ListBoxItem Content="1"/>
            <ListBoxItem Content="2"/>
        </ListBox>
    </Grid>
</Window>