Search This Blog

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>




Wednesday, November 4, 2015

Label WPF Example

WPF > Controls  > ContentControl > Label

This control represents the text label for a control and provides support for access keys.

Example

The following example shows how to create a Label that has an access key and uses a binding to set the target.

<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">
    <Grid>
        <TextBox Name="textBox" Margin="49,0,281,291"/>
        <Label Target="{Binding ElementName=textBox}">_Name</Label>
    </Grid>

</Window>

Tuesday, November 3, 2015

RadGridView WPF disable cell or row

WPF > RadGridView > Disable

<Style x:Key="disabledCell" TargetType="telerik:GridViewCell">
<Setter Property="IsEnabled" Value="False"/>
</Style>

<telerik:GridViewDataColumn 
        DataMemberBinding="{Binding Name}"
        CellStyle="{StaticResource disabledCell}"
/>


To disable all the rows you need to apply a Style targeting the GridViewRow element.

<Style TargetType="telerik:GridViewRow">
       <Setter Property="IsEnabled" Value="False"/>
</Style>

Tuesday, September 15, 2015

WPF MVVM Basic quick start tutorial in C#

WPF >MVVM Basic quick start tutorial C#

In MVVM (Model-View-ViewModel) 

  • model knows nothing
  • view model knows only about model
  • view knows only about view model

Project structure


Project.cs

using System.ComponentModel;
namespace MVVM
{
    public class Project : INotifyPropertyChanged
    {
        private string projectCode;
        private string projectDescription;

        public string ProjectCode
        {
            get
            {
                return projectCode;
            }
            set
            {
                projectCode = value;
                OnPropertyChanged("ProjectCode");
            }
        }

        public string ProjectDescription
        {
            get
            {
                return projectDescription;
            }
            set
            {
                projectDescription = value;
                OnPropertyChanged("ProjectDescription");
            }
        }

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}

Projects.xaml

<Window x:Name="ProjectsWindow" x:Class="MVVM.View.Projects"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Projects" Height="300" Width="300" Loaded="ProjectsWindow_Loaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Button Content="Save" Grid.Row="0"  HorizontalAlignment="Left" Name="btnUpdate"
                VerticalAlignment="Top"
                Command="{Binding Path=SaveCommand}"  />
        <ListView Name="ProjectList" Grid.Row="1"  ItemsSource="{Binding Projects}"  >
            <ListView.View>
                <GridView x:Name="grdProject">
                    <GridViewColumn Header="Code" DisplayMemberBinding="{Binding ProjectCode}" Width="Auto" />
                    <GridViewColumn Header="Description" DisplayMemberBinding="{Binding ProjectDescription}" Width="Auto" />
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

Projects.xaml.cs

using System.Windows;
namespace MVVM.View
{
    /// <summary>
    /// Interaction logic for Projects.xaml
    /// </summary>
    public partial class Projects : Window
    {
        public Projects()
        {
            InitializeComponent();
        }

        private void ProjectsWindow_Loaded(object sender, RoutedEventArgs e)
        {
            ProjectViewModel projectViewModel = new ProjectViewModel();
            DataContext = projectViewModel;
        }
    }
}

ProjectViewModel.cs

using System;
using System.Collections.Generic;
using System.Windows.Input;
namespace MVVM
{
    public class ProjectViewModel
    {
        RelayCommand saveCommand;
        private IList<Project> projectList;
        public ProjectViewModel()
        {
            projectList = new List<Project>
            {
                new Project{ProjectCode="P1", ProjectDescription="Project1"},
                new Project{ProjectCode="P2", ProjectDescription="Project2"}
            };
         
        }
        public IList<Project> Projects
        {
            get { return projectList; }
            set { projectList = value; }
        }

        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new RelayCommand(
                        param => this.Save(),
                        param => this.CanSave
                        );
                }
                return saveCommand;
            }
        }

        public void Save()
        {
           // implement your Save function
        }

        bool CanSave
        {
            get { return true; }
        }

    }
}

RelayCommand.cs

using System;
using System.Diagnostics;
using System.Windows.Input;

namespace MVVM
{
    /// <summary>
    /// A command whose sole purpose is to
    /// relay its functionality to other
    /// objects by invoking delegates. The
    /// default return value for the CanExecute
    /// method is 'true'.
    /// </summary>
    public class RelayCommand : ICommand
    {
        #region Fields

        readonly Action<object> _execute;
        readonly Predicate<object> _canExecute;       

        #endregion // Fields

        #region Constructors

        /// <summary>
        /// Creates a new command that can always execute.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        public RelayCommand(Action<object> execute)
            : this(execute, null)
        {
        }

        /// <summary>
        /// Creates a new command.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;          
        }

        #endregion // Constructors

        #region ICommand Members

        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }

        #endregion // ICommand Members
    }
}

App.xaml

<Application x:Class="MVVM.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="View/Projects.xaml">
    <Application.Resources>
        
    </Application.Resources>
</Application>

Run the application