WPF: Rotation and Scale Transform Example



This is a simple example which explains how we can apply render and scale transform.
In this example we have an image and we will apply scale and render transform to this image.
Render transform will help us to rotate the image at specific angle. Similarly, Scale Transform will reduce or increase the size of image in X or Y axis direction.

In the example we have used: 
1)      Image 
2)      3 Scrollbars which are bounded with angle of rotation, scale x and scale y property.

Here is the XAML source code for the same:


Set selected row index in DevExpress Grid Control by C# code

In DevExpress grid control there is no direct property to set selected row index from C# code if you need to do so. To achieve this functionality one needs to define TableView for the DevExpress grid control. The TableView is defined under <dxg:GridControl.View></dxg:GridControl.View> tag.

To add DevExpress Grid you will require
xmlns:dxg='http://schemas.devexpress.com/winfx/2008/xaml/grid'

In the TableView there is a property called "FocusedRowHandle" which is used to set the selected row index in the GridControl

Consider Sample XAML Written Below
<StackPanel>
   <dxg:GridControl Height='300' Grid.Column='0' Grid.Row='1' x:Name='grdDetail' IsFilterEnabled='False' IsManipulationEnabled='False'> <dxg:GridControl.Columns> <dxg:GridColumn Header='SeqNo' AllowResizing='False' Width='30'  FieldName='[SeqNo]' HorizontalHeaderContentAlignment='Center' AllowSorting='False'>               <dxg:GridColumn.CellTemplate>                   <DataTemplate>                     <TextBlock HorizontalAlignment='Left' VerticalAlignment='Center'  Text='{Binding Path=RowData.Row.[SeqNo]}'></TextBlock>                   </DataTemplate>               </dxg:GridColumn.CellTemplate>                             </dxg:GridColumn>          <dxg:GridColumn Header='RollNo' FieldName='[RollNo]' AllowSorting='False'>               <dxg:GridColumn.CellTemplate>                   <DataTemplate>                    <TextBlock HorizontalAlignment='Right' VerticalAlignment='Center'  Text='{Binding Path=RowData.Row.[RollNo]}'></TextBlock>                   </DataTemplate>              </dxg:GridColumn.CellTemplate>          </dxg:GridColumn>          <dxg:GridColumn Header='Name' FieldName='[Name]' AllowSorting='False'>               <dxg:GridColumn.CellTemplate>                   <DataTemplate>                    <TextBlock HorizontalAlignment='Center' VerticalAlignment='Center' Text='{Binding Path=RowData.Row.[Name]}'></TextBlock>                   </DataTemplate>               </dxg:GridColumn.CellTemplate>          </dxg:GridColumn>          <dxg:GridColumn Header='Marks' FieldName='[Marks]' Width='50'  HorizontalHeaderContentAlignment='Center' AllowSorting='False'>                <dxg:GridColumn.CellTemplate>                  <DataTemplate>                    <TextBlock HorizontalAlignment='Right' VerticalAlignment='Center'  Text='{Binding Path=RowData.Row.[Marks]}'></TextBlock>                   </DataTemplate>                </dxg:GridColumn.CellTemplate>          </dxg:GridColumn>          <dxg:GridColumn Header='Rank' FieldName='[Rank]' Width='50'  HorizontalHeaderContentAlignment='Center' AllowSorting='False'>               <dxg:GridColumn.CellTemplate>                 <DataTemplate>                 <TextBlock HorizontalAlignment='Right' VerticalAlignment='Center'  Text='{Binding Path=RowData.Row.[Rank]}'></TextBlock>                   </DataTemplate>                </dxg:GridColumn.CellTemplate>          </dxg:GridColumn>        </dxg:GridControl.Columns>        <dxg:GridControl.View>        <dxg:TableView Name='tblViewDetail' AllowColumnFiltering='False'  AllowEditing='False' AllowGrouping='True' AllowResizing='True'  AllowSorting='True' AutoWidth='True' ShowGroupPanel='False'  ShowIndicator='False' ShowVerticalLines='False'>         </dxg:TableView>        </dxg:GridControl.View>    </dxg:GridControl>    <StackPanel Orientation="Horizontal">        <Button Content="Select 1st Row" Click="Button_Click" x:Name="Sel1"></Button>        <Button Content="Select 2nd Row" x:Name="Sel2" Click="Sel2_Click"></Button>        <Button Content="Select 3rd Row"  x:Name="Sel3" Click="Sel3_Click"></Button>   </StackPanel> </StackPanel>

Now In this XAML we will add ItemSources by C# Code as well as selected row will be set by c# code. The C# Code will be as followed

WPF HyperLink Button using Style and Control Template




WPF: HyperLink Button using Style and Control Template

Using this post, you will be able to create a button, which will look like Hyperlink.
To achieve this you need to create a style for a button and in that style you need to modify the template of the button so that it looks like hyperlink.

Also, mostly we see that in hyperlink, the color changes whenever the mouse hovers on it. So using Control Template triggers, we will change the foreground color of the hyperlink.

Here is the Hyperlink button style which you need to define in the Resources section of XAML.


WPF: Image Button using Control Template



WPF: Image Button using Control Template

In WPF, for better UI representation we mostly use images in our application.
So most common place where we use images is in place of buttons.
Mostly we represent action buttons such as add,delete etc with some image which represents add or delete functionality.

Here we have taken an example of Add button and we have used Control Template for modifying the button to show as image instead of normal button.


WPF: Specify Combo Box ItemsSource from XAML

Use of x:Array in XAML
To define itemsource from XAML using x:Array, we need to add the following namespaces.
xmlns:sys="clr-namespace:System;assembly=mscorlib"

We can create an array of string, or any other simple data type in Resources section.
We have created an array of string in the following example:
<x:Array x:Key="StringArrayResource" Type="sys:String">
<sys:String>Combo Value1</sys:String>
<sys:String>Combo Value2</sys:String>
<sys:String>Combo Value3</sys:String>
</x:Array>


In the above example we can see that Type is specified as sys:String where 'sys' is the System NameSpace from mscorlib assembly.
Note we have given key "StringArrayResource" for the string array.
Now the main part, Binding Combo Box ItemsSource with the resource defined.
<ComboBox x:Name="comboBox" ItemsSource="{StaticResource StringArrayResource}" Height="30" Width="90">          
</ComboBox>


The Combo Box will look like this.


WPF: Clear SelectedValue of Combo box on delete button press and Handling Other keys and Cut, Copy, Paste Events

You can use following code to Delete the SelectedValue of combobox on delete  button press event with preventing other key inputs. You can also handle cut copy and paste event on the combobox by using below code.

Overview of inheritance with C# : part 2

Overriding

If we create base class method to virtual and override the method of the derived class, now when we instantiate the base class object to derived class and call the method with respect to the object then method of derived class will be invoked.

Sample Code

Overview of inheritance with C# : part 1

When base class and derived class both have same method but base class's method is not virtual, and derived class's method is not overridden, then if we instantiate the object of base class to derived class and call the method with respect to the object created, the method written in base class will be invoked.

Sample code

namespace Inheritance { class Program { static void Main(string[] args) { BaseDemo bd = new DerivedDemo(); bd.PrintString(); Console.ReadLine(); } } class BaseDemo { public BaseDemo() { } public void PrintString() { Console.WriteLine("Base Demo Invoked"); } } class DerivedDemo : BaseDemo { public DerivedDemo() { } public void PrintString() { Console.WriteLine("Derived Demo Invoked"); } } }
Output
Base Demo Invoked

Compilation of above code will generate warning that
"Warning 1 'Inheritance.DerivedDemo.PrintString()' hides inherited member Inheritance.BaseDemo.PrintString()'. Use the new keyword if hiding was intended."

Binding Errors in WPF / XAML


WPF doesn’t raise exceptions to notify programmer about data binding problems. If you specify an element or a property that doesn’t exist, you won’t receive any indication; instead, the data will simply fail to appear
in the target property. At first glance, this seems like a debugging nightmare. Fortunately, WPF does output trace information that details binding failures. This information appears in Visual Studio’s Output window
when you’re debugging the application. For example, if you try to bind to a nonexistent property, you’ll
see a message like this in the Output window:

Data Trigger to Inverse the value of IsChecked Property of checkbox for binding

Many times we may require to enable / disable controls in WPF on the check state of a checkbox in the window. In XAML we want to enable our control if checkbox is checked and disable if it is unchecked, this can be done very easily by using below binding


<StackPanel Orientation="Horizontal"> <CheckBox x:Name="chkHandler" Content="Enable Controls"> </CheckBox> <TextBlock Width="50" />
<TextBox x:Name="Sample" Text="Sample Text Box" 
IsEnabled="{Binding Path=IsChecked, ElementName=chkHandler}">
</TextBox> </StackPanel>