Search This Blog

Wednesday, October 30, 2013

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;
}