Archive for the ‘WPF’ Category

WPF Mouse bindings

Friday, March 27th, 2009

The following article discusses the WPF command binding feature with relation to Mouse clicks.

One of WPF powerful features is the binding of commands to controls. If you have used the MVVM design pattern, you will know how useful, powerful, easy, efficient and robust the application gets with WPF commands.
(more…)

WPF Binding Datagrid to DataTable

Wednesday, December 17th, 2008

The following code snippet demonstrates different ways to bind a ADO.Net DataTable to a WPF Datagrid.

WPF Datagrid can be found at this location - Codeplex

1. Setting the ItemsSource of the Datagrid to the DataTable’s DefaultView
XAML,

<Window x:Class="WpfDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dtgrd="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
    Title="Window1" Height="400" Width="400">
    <Grid  Name="_maingrid">
        <dtgrd:DataGrid
                         x:Name="_dataGrid"
                         ColumnHeaderHeight="25"
                         AutoGenerateColumns="True"
                              >
        </dtgrd:DataGrid>
    </Grid>
</Window>

Code-behind,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;  

namespace WpfDemo
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private DataSet _ds;  

        public Window1()
        {
            InitializeComponent();
        }  

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);  

            ds = new DataSet();
            DataTable dt = new DataTable();
            ds.Tables.Add(dt);  

            DataColumn cl = new DataColumn("Col1", typeof(string));
            cl.MaxLength = 100;
            dt.Columns.Add(cl);  

            cl = new DataColumn("Col2", typeof(string));
            cl.MaxLength = 100;
            dt.Columns.Add(cl);  

            DataRow rw = dt.NewRow();
            dt.Rows.Add(rw);
            rw["Col1"] = "Value1";
            rw["Col2"] = "Value2";  

            _datagrid.ItemsSource = ds.Tables[0].DefaultView;
        }
    }
}

2. Setting the DataContext to the table and then binding ItemsSource to the DataContext
XAML,

<Window x:Class="WpfDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dtgrd="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
    Title="Window1" Height="400" Width="400">
    <Grid  Name="_maingrid">
        <dtgrd:DataGrid
                         x:Name="_dataGrid"
                         ItemsSource="{Binding Path=.}"
                         ColumnHeaderHeight="25"
                         AutoGenerateColumns="True"
                              >
        </dtgrd:DataGrid>
    </Grid>
</Window>

Code-behind,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;  

namespace WpfDemo
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private DataSet _ds;  

        public Window1()
        {
            InitializeComponent();
        }  

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);  

            ds = new DataSet();
            DataTable dt = new DataTable();
            ds.Tables.Add(dt);  

            DataColumn cl = new DataColumn("Col1", typeof(string));
            cl.MaxLength = 100;
            dt.Columns.Add(cl);  

            cl = new DataColumn("Col2", typeof(string));
            cl.MaxLength = 100;
            dt.Columns.Add(cl);  

            DataRow rw = dt.NewRow();
            dt.Rows.Add(rw);
            rw["Col1"] = "Value1";
            rw["Col2"] = "Value2";  

            _datagrid.DataContext = ds.Tables[0];
        }
    }
}

Have fun.

WPF Dispatcher CheckAccess not displaying in intellisense

Thursday, October 16th, 2008

This one really struck me hard. I am doing a threading scenario with some UI feedback marshaled back on the UI thread. And you won’t believe it, I am amazed to find CheckAccess missing from the Dispatcher object. Intellisense wouldn’t just show me CheckAccess(). Rubbing my eyes and again checking, it still seems to have vanished…

(more…)

WPF Menu Shortcut and Hotkey

Friday, July 18th, 2008

We often use Menu items in our forms having Hot keys and shortcut keys. This is a little bit tricky to use in WPF.
(more…)

WPF Multiline Textbox

Wednesday, June 11th, 2008

The following code snippet demonstrates how to have a multiline WPF TextBox.

(more…)

WPF Setting Grid row and column number for a control being added to Grid

Saturday, June 7th, 2008

In normal case we generally add our controls to the WPF Grid in XAML setting the Row and Column value. If you want to add a control to the Grid at runtime from Code-behind, the way to do it will be,

yourGrid.Children.Add(yourControl);

(more…)

WPF Multitrigger

Thursday, May 15th, 2008

This article discusses and demonstrates the implementation of a trigger in WPF which has multiple conditions.

Triggers in WPF are very important and handy features. Triggers and styles add to WPF’s bag a rich set of functionality. Triggers are like event handlers which we define in XAML’s DateTemplates and styles.Triggers allow us to define several Setter elements in a style. The trigger includes a condition, and when that condition is true, the Setters defined in the trigger will be applied. When the condition is false, the Setters are ignored.

Here is a sample trigger,

< Style.Triggers >
    < Trigger Property="IsEnabled" Value="False" >
      < Setter Property="Visibility" Value="Collapsed" / >
    < /Trigger >
< /Style.Triggers >

The above example demonstrates a Style trigger which fires when the IsEnabled property is false. The resulting controls Visibility property will be set to Collapsed.

Moving ahead, what if our condition is based on multiple criteria? Here comes the use of MultiTriggers.

Here is a sample Multi-Trigger,

< MultiTrigger >
      < MultiTrigger.Conditions >
        < Condition Property="Content" Value="a" / >
        < Condition Property="IsMouseOver" Value="True" / >
      < /MultiTrigger.Conditions >
      < Setter Property="Visibility" Value="Collapsed" / >
< /MultiTrigger >

The above trigger will fire when Content has a value of “a” and mouse is over the control.

Have fun.

WPF Timer

Wednesday, March 26th, 2008

This article discusses the WPF DispatcherTimer.

Most of us are from a background of C# (or VB.Net). You must have some point of time come across a scenario for performing a repetitive GUI operation after certain period of time. The solution in most cases has been the WinForm’s Timer.

What about WPF ? Does it have a Timer ?
(more…)

WPF Formatting in XAML Binding

Wednesday, December 5th, 2007

Ever wondered how to format a string in a XAML binding ?
This following code snippet demonstrates how to format a string in a databinding.
(more…)

WPF Finding color of BorderBrush

Monday, November 12th, 2007

This article demonstrates the way to work with WPF BorderBrush.

The Border control in WPF is used to draw a border along the edges of a control. The Border control has a property named BorderBrush which is the drawing brush for the border.

The way we declare the BorderBrush in XAML is different than through code. As we know, XAML automatically converts string to a specific type.
The following code demonstrates use of the Border element,

<StackPanel>
    <Border BorderBrush="Blue">
       <TextBox>Dummy text</TextBox>
    </Border>
</StackPanel>

The above code has a StackPanel containing a Border for a TextBox. The BorderBursh is assigned a Blue color. This means, the Border will be a SolidColorBrush of Blue color. SolidColorBrush is the default WPF Brush.

Now, we will see how to interact with the BorderBrush in code-behind. The scenario we will look at is, finding the current color of the BorderBrush and if Blue then changing it to Red. The following code demonstrates the same,

Color clr = (brdr.BorderBrush as SolidColorBrush).Color;
if (clr.Equals(Colors.Blue))
    brdr.BorderBrush = new SolidColorBrush(Color.Red);

Have fun.