Search This Blog

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

Display SplashScreen From Resource File WPF


WPF > Windows > SplashScreen 

SplashScreen shows an image when a WPF application starts or in a startup window.
Call the Show method to display the splash screen.
Supported image format: BMP, GIF, JPEG, PNG, TIFF.

Example:

var splashScreen = new SplashScreen( resourceName : "Resources/loading1.gif");
splashScreen.Show(false);

Monday, October 28, 2013

Set StartUp Window or Page WPF

WPF  > Windows > Application > StartupUri

StartupUri gets or sets a UI that is automatically shown when an application starts.

Example

StartUp Window or Page WPF

Open App.xaml file and change StartupUri.

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

Data Binding in WPF

WPF  Data Binding

With Data binding in WPF you can take data from any property of an object and bind it to other dependency property of another object.
Data binding copies a value from a source object to a property on a destination object.
WPF object has change notification.
When the source change, the destination property will automatically be updated.

Simple example:

<Grid>
        <Grid.RowDefinitions >
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <TextBox Name="Source" Grid.Row="0" >Source</TextBox>
        <TextBlock Grid.Row="1">
            <TextBlock.Text>
                <Binding ElementName="Source" Path="Text"  />
            </TextBlock.Text>
        </TextBlock>
</Grid>

Binding modes

OneWay
  • source -> destination
OneWayToSource
  • destination - > source
OneWayToSource
  • destination - > source
OneTime

  • source - > destination (the property is initially set, but updates to the source are not copied to the destination)
TwoWay

  • source -> destination
  • destination - > source
Other examples:

SQL Server datagrid view,update, delete data


 

Printing in WPF

WPF > Printing

Windows Presentation Foundation (WPF) have a rich set of printing and print system management APIs. 
The core of this new functionality is the new XML Paper Specification (XPS) file format and the XPS print path.

Example: Printing a Visual
XAML:
<Window x:Class="Printing.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 x:Name="Grid">
        <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Margin="91,63,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="75,197,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
     </Grid>
</Window>
Code:
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)

{
    printDialog.PrintVisual(Grid, "My First Print Example");
}

PrintVisual Method WPF

WPF > Printing > PrintVisual

PrintVisual method prints a visual  object to the PrintQueue.

Parameters
  • visual: object to be print
  • description: description of the job that is to be printed

Examples:

Print DataGrid WPF

WPF > Printing > DataGrid

Print DataGrid WPF Example

PrintDialog printDlg = new PrintDialog();
printDlg.PrintVisual(dgProducts, "Grid Printing.");