Terje Isaksen Developer blog

2010-05-04 (Tuesday)

WPF: Missing tag gives exception “Items collection must be empty before using ItemsSource.”

Filed under: Developing — Terje Isaksen @ 14:14
Tags: , , ,

If you have a datagrid with columns and misses to use the tag <toolkit:DataGrid.Columns> – then you will get the exception “Items collection must be empty before using ItemsSource.”

Wrong:

<toolkit:DataGrid >
     <toolkit:DataGridTemplateColumn >
        <toolkit:DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
               <StackPanel>
                  <toolkit:DatePicker  SelectedDate="{Binding Date, Mode=Twoway, StringFormat=yyyy-MM-dd, UpdateSourceTrigger=PropertyChanged}" />
                </StackPanel>
           </DataTemplate>
        </toolkit:DataGridTemplateColumn.CellTemplate>
     </toolkit:DataGridTemplateColumn>
</toolkit:DataGrid> 

Correct:

<toolkit:DataGrid >
  <toolkit:DataGrid.Columns>
     <toolkit:DataGridTemplateColumn >
        <toolkit:DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
               <StackPanel>
                  <toolkit:DatePicker  SelectedDate="{Binding Date, Mode=Twoway, StringFormat=yyyy-MM-dd, UpdateSourceTrigger=PropertyChanged}" />
                </StackPanel>
           </DataTemplate>
        </toolkit:DataGridTemplateColumn.CellTemplate>
     </toolkit:DataGridTemplateColumn>
  </toolkit:DataGrid.Columns>
</toolkit:DataGrid> 

2010-02-14 (Sunday)

Projects: WPF Artist Phase One

Filed under: Developing,Project — Terje Isaksen @ 00:08
Tags: , , , ,

There is an artform which basically is when people throw paint, (“Abstract Expressionist – Kinetic Abstraction”, example: http://www.ecomall.com/zatar/) and this of course can be done with a computer as well.

Sample

"Art" made by this piece of code



WPF:

<Window x:Class="Graph.Window1"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 Title="Window1" >
 <DockPanel LastChildFill="True">
 <StackPanel DockPanel.Dock="Top" Orientation="Horizontal"></StackPanel>
 <Grid x:Name="deGrid" DockPanel.Dock="Bottom" Background="AntiqueWhite" Height="800" Width="1600">

 </Grid>
 </DockPanel>
</Window>

Code behind:

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Graph
{
 /// <summary>
 /// Interaction logic for Window1.xaml
 /// </summary>
 public partial class Window1 : Window
 {
 DispatcherTimer timer = new DispatcherTimer();
 Random R = new Random(  );
 bool running = false;

 public Window1() {
 InitializeComponent();
 timer.Interval = TimeSpan.FromMilliseconds( 500 );
 timer.Tick += new EventHandler( timer_Tick );
 timer.Start();
 }

 private void timer_Tick( object sender , EventArgs e ) {
 Polyline imgNew = GetDrawing();
 this.deGrid.Children.Add( imgNew );
 this.deGrid.UpdateLayout();
 }

 Byte RandomByte() {
 Byte u = 0;  bool b = byte.TryParse( R.Next( 255 ).ToString() , out u );
 return u;
 }

 Color GetColor()  {
 byte A = RandomByte(); byte R = RandomByte();  byte G = RandomByte();   byte B = RandomByte();
 return Color.FromArgb( A , R , G , B );
 }

 Polyline GetDrawing(  ) {
 Color drawColor = GetColor();
 Brush drawBrush =  new SolidColorBrush(drawColor);
 Polyline u = new Polyline();
 u.StrokeThickness = R.Next(10);
 u.Stroke = drawBrush;

 double x0 = R.NextDouble() * deGrid.Width ;
 double y0 = R.NextDouble() * deGrid.Height ;
 double I = R.NextDouble() * 10000 ;

 for (int i=0; i<I; i++) {
 Point p = new Point( x0 , y0 );  u.Points.Add(p);

 x0 = x0 + R.Next( R.Next(10) ) - R.Next( R.Next(10) );
 if (x0<0) { x0 = 0; }  if (x0>deGrid.Width ) { x0 = deGrid.Width; }

 y0 = y0 + R.Next( R.Next(10) ) - R.Next( R.Next(10) );
 if (y0<0) { y0 = 0; }  if (y0>deGrid.Height) { y0 = deGrid.Height;}
 }
 u.ClipToBounds = true;
 return u;
 }

 }
}

If you become a well known artist using this software, please let me know. 😀

2010-02-11 (Thursday)

WPF Free sample of Vista style Icons

Filed under: Developing — Terje Isaksen @ 18:02
Tags: ,

http://www.grafile.com/presentation/Vista_Toolbar_library.html

WPF Cursors

Filed under: Developing — Terje Isaksen @ 18:00
Tags:

To set the mouse cursor on a window

this.Cursor = Cursors.Wait;

WPF Menus in code

Filed under: Developing — Terje Isaksen @ 12:14
Tags: , ,

A menu is created in XAML with

<Menu Height="22" Name="menu1" VerticalAlignment="Top" >
            <MenuItem Header="Alfa" ><MenuItem>
</Menu>

To do this in code:

public MenuItem MakeMenu()  {
        MenuItem mmm1 = new MenuItem();    mmm1.Header = "Subsubmenu";
        mmm1.Click += new RoutedEventHandler( mmm1_Click );
        MenuItem mm1 = new MenuItem();        mm1.Header = "Submenu";        mm1.Items.Add( mmm1 );
        MenuItem m1 = new MenuItem();        m1.Header = "Menu";        m1.Items.Add( mm1 );

return m1;
}
void mmm1_Click( object sender , RoutedEventArgs e )     {         MessageBox.Show( "Hepp" );        }

and then in the constructor of the window:

 public Window1()        {
         InitializeComponent();
         menu1.Items.Add( MakeMenu() );
}

2010-02-03 (Wednesday)

Mask on Textbox

Filed under: Developing — Terje Isaksen @ 17:16
Tags: , ,

Ruben Hakopian shows us how it is done:
http://rubenhak.com/?p=8

2010-02-01 (Monday)

GotFocus for a grid

Filed under: Developing — Terje Isaksen @ 12:24
Tags:

Instead of having one GotFocus-trigger for each textbox in a grid, you can have it at Grid-level.

XAML:

<Window x:Class="Test.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        >
       <Grid GotFocus="Grid_GotFocus" LostFocus="Grid_LostFocus">
             <TextBox Height="23" Margin="12,12,12,0" x:Name="textBox1" VerticalAlignment="Top" />
             <TextBox Margin="12,41,12,0" x:Name="textBox2" Height="23" VerticalAlignment="Top" />
       </Grid>
</Window>

Codebehind:

using System.Windows;
using System.Windows.Controls;

namespace Test {
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window  {
         public Window1() { InitializeComponent(); }
                private void Grid_GotFocus( object sender , RoutedEventArgs e ) {
                        if ( e.Source is TextBox ) {
                            TextBox t = e.Source as TextBox;
                            MessageBox.Show( t.Name + " Got focus" );
                        }
                }
                private void Grid_LostFocus( object sender , RoutedEventArgs e ) {
                        if ( e.Source is TextBox ) {
                            TextBox t = e.Source as TextBox;
                            MessageBox.Show( t.Name + " Lost focus" );
                        }
                }
        }
}

Useful for validations.

For example – if you want to process names and values of all textboxes as soon as user is leaving a textbox, you can in the LostFocus eventhandler do this:

        private void Grid_LostFocus( object sender , RoutedEventArgs e )  {
            if ( e.Source is TextBox )   {
                int count = VisualTreeHelper.GetChildrenCount(theGrid);
                string u = string.Empty;
                for ( int i = 0; i < count; i++ )      {
                      object o = VisualTreeHelper.GetChild( theGrid , i );
                      if ( o is TextBox ) {
                          TextBox t = o as TextBox;
                           u += t.Name + ": " + t.Text + Environment.NewLine;
                      }
                }
               MessageBox.Show( u );
            }
        }

2009-11-24 (Tuesday)

Numeric to string

Filed under: Developing — Terje Isaksen @ 17:25
Tags: ,

Binding a textbox to a numeric source and formatting it is done by

<TextBox Text="{Binding Path=Fieldname, StringFormat=formatstring}"/>

where “formatstring” can for example be any of following:

Fn – fixed point number  – where n= number of decimals
Pn – percentage (value is multiplied with 100 and displayed with a %-symbol) – where n = number of decimals
Dn – integer – where n= number of digits
Nn – number with decimals and group separators, where n=number of decimals
Xn – hexadecimal integer – where n= number of digits

It can also be a string of custom numeric format characters inside a pair of single quotes which for example can be:

0 – zero placeholder, gives a digit if present – zero otherwise
# – digit placeholder, gives a digit if present, nothing otherwise
. – decimal point – determines where the decimal seperator should be
, – group separator – gives the localized group separator. Can also be used at the end to divide the number with 1000 for each comma
% – percentage placeholder – if at the end – multiplies number with 100 and gives percentage symbol at end

See more at:

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

http://blogs.msdn.com/llobo/archive/2008/05/19/wpf-3-5-sp1-feature-stringformat.aspx


2009-11-03 (Tuesday)

WPF Refresh window

Filed under: Developing — Terje Isaksen @ 17:18
Tags:

Is done by

this.UpdateLayout();

2009-10-30 (Friday)

WPF DockPanel

Filed under: Developing — Terje Isaksen @ 16:56
Tags:

A good way to stick things to the sides of the window.

The trick is to put the part that should expand at the end of the XAML-code and to set

LastChildFill="True"

in the DockPanel -tag.

See this excellent explaination on WpfTutorial:
http://www.wpftutorial.net/DockPanel.html

Next Page »

Blog at WordPress.com.