Search This Blog

Thursday, October 31, 2013

WPF RepeatButton

WPF > Controls  > ContentControl > RepeatButton

WPF RepeatButton raises its Click event repeatedly from the time it is pressed until it is released.

Properties
  • Interval: gets or sets the amount of time, in milliseconds, between repeats once repeating starts
  • Delay: gets or sets the amount of time, in milliseconds, the RepeatButton waits while it is pressed before it starts repeating
WPF RepeatButton Example:

XAML:

<Grid>
        <RepeatButton Margin="10,10,0,0" VerticalAlignment="Top"
              Delay="500" Interval="500"
              HorizontalAlignment="Left"                      
              x:Name="btnIncrease"  Width="352" Height="30" Click="GrowButton_Click">
            RepeatButton: Click on this button and keep mouse pressed
        </RepeatButton>
        <TextBlock x:Name="txt" HorizontalAlignment="Left" Margin="10,55,0,0" TextWrapping="Wrap" Text="TextBlock"
                   VerticalAlignment="Top" Height="18" Width="352" RenderTransformOrigin="0.5,0.5">
        </TextBlock>
</Grid>

C# Code:

public partial class MainWindow : Window
    {
        int k = 0;
        public MainWindow()
        {
            InitializeComponent();
        }
        private void GrowButton_Click(object sender, RoutedEventArgs e)
        {
            k += 1;
            txt.Text = k.ToString() ;
        }
    }

Wednesday, October 30, 2013

WPF ScrollViewer

WPF > Controls  > ContentControl > ScrollViewer

WPF ScrollViewer control enables scrolling of content.
Sometimes the content is larger than a computer screen display area. This control encapsulates ScrollBar elements and a content container.

WPF ScrollViewer Example:

XAML:

<Window x:Class="ScrollViewer.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="MainWindow" Height="406" Width="411">
    <ScrollViewer HorizontalScrollBarVisibility="Auto"   VerticalScrollBarVisibility="Auto">
       <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left">
           <TextBlock TextWrapping="Wrap">ScrollViewer Example. Resize the window to see functionality</TextBlock>
           <Rectangle Fill="Blue" Width="400" Height="400"></Rectangle>
       </StackPanel>
   </ScrollViewer>
</Window>


WPF Window

WPF > Controls  > ContentControl > Window

A Windows Presentation Foundation (WPF) Window represents interaction between a user and a standalone application.
WPF window has two areas:
  • A non-client area (title, icon, buttons, border)
  • A client area (application content)

WPF Window Example:

In this example we will create a transparent window with opacity of 80%.

XAML:

<Window x:Class="ButtonExample.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" Opacity="0.8" AllowsTransparency="True" WindowStyle="None" Background="#FFEEE4E4"
        WindowStartupLocation="CenterScreen">
    <Grid>
        <Button Content="Close" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    </Grid>
</Window>

C# Code:

private void Button_Click(object sender, RoutedEventArgs e)
{  
  this.Close();
}

WPF Checkbox

WPF > Controls  > ContentControl > CheckBox

WPF Checkbox  is a control that a user can check/uncheck and clear


WPF Checkbox Example:

XAML:

<Grid>
   <TextBox x:Name="txt1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBox"     Width="215" Margin="0,27,0,220"/>
<CheckBox x:Name="chkBox" Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Top" Checked="chkBox_Checked" Unchecked="chkBox_Unchecked" Indeterminate="chkBox_Indeterminate" IsThreeState="True"/>
</Grid>

C# Code:

private void chkBox_Checked(object sender, RoutedEventArgs e)
{
   txt1.Text = "chkBox checked";
}
private void chkBox_Unchecked(object sender, RoutedEventArgs e)
{
   txt1.Text = "chkBox unchecked";
}
private void chkBox_Indeterminate(object sender, RoutedEventArgs e)
{
   txt1.Text = "chkBox indeterminate";
}



WPF Button

WPF > Controls  > ContentControl > Button

WPF Button represents a Windows button control.
The main event is Click event.

WPF Button Example:

XAML:

<Window x:Class="ButtonExample.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 x:Name="txt1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBox" Width="215" Margin="0,27,0,220"/>
        <Button x:Name="btn1" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="btn1_Click"/>
    </Grid>
</Window>

C# Code:

private void btn1_Click(object sender, RoutedEventArgs e)
{
   txt1.Text = "Button was clicked";
}  



Other examples:


ContentControl WPF

WPF > Controls  > ContentControl

ContentControl is a control with a single piece of content of any type and supports only one child.

List of ContentControl:

Windows Presentation Foundation (WPF) Controls

WPF > Controls

A control is any class that represents a visible object in an application.
WPF comes with a lot of UI components such as Button, Label, TextBox, ListBox, etc.
In WPF a class does not need to inherit from the Control class to have a visible presence.
ControlTemplate  allows a consumer of a control to change the control's appearance without having to create a new subclass.


ItemsControl WPF

WPF > ControlsItemsControl

ItemsControl in WPF is a type of Control that can contain multiple items like strings, objects, etc.

To specify the collection used to generate the content of your ItemsControl use  Items or the ItemsSource property.

ItemsSource:
  • used to display a data collection or to bind an ItemsControl to a collection
  • any type that implements IEnumerable
  • when set the Items collection is set to read only 
Items:
  • can have different types (ListBox can contain strings, images)
  • can add items to the collection directly
Controls

Programmatically fill TreeView items WPF

WPF > ItemsControl > TreeView > Add items to TreeView

Example of programmatically add items to TreeView control in WPF.

XAML:

        <TreeView x:Name="tvMain"  Grid.Column="0">
            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <EventSetter Event="MouseLeftButtonUp" Handler="treeViewItem_MouseLeftButtonUp"/>
                </Style>
            </TreeView.ItemContainerStyle>
        </TreeView>

 
Code:
 
  // fill Tree

            TreeViewItem tviSet = new TreeViewItem() { Header = "Settings"  };
            TreeViewItem tviProd = new TreeViewItem() { Header = "Products" };
            tviProd.Tag = "Products";

            TreeViewItem tviComp = new TreeViewItem() { Header = "Companies" };
            tviComp.Tag = "Companies";

            tviSet.Items.Add(tviProd);
            tviSet.Items.Add(tviComp);
            tviSet.IsExpanded = true;

            tvMain.Items.Add(tviSet);

       

Tuesday, October 29, 2013

Handle Mouse Click Event on TreeViewItem TreeView WPF

WPF > ItemsControl > TreeView > Events > MoseLeft

How to capture Left Mouse Click on TreeView item control in WPF?

Use ItemContainerStyle and EventSetter



Example:

XAML

<TreeView x:Name="tvMain>
           <TreeView.ItemContainerStyle>
             <Style TargetType="{x:Type TreeViewItem}">
                       <EventSetter Event="MouseLeftButtonUp" Handler="treeViewItem_MouseLeftButtonUp"/>
              </Style>
       </TreeView.ItemContainerStyle>
</TreeView>

c# Code:

private void treeViewItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
      TreeViewItem tvi = (TreeViewItem)sender;
      e.Handled = true;
}