C# Value Types Default Values

Published 2/25/2016 by Christian in Code
Tags:

According to: https://msdn.microsoft.com/da-dk/library/83fhsxwc.aspx

the default values of C# Value Types, are as followes:

Value type

Default value

bool

false

byte

0

char

'\0'

decimal

0.0M

double

0.0D

enum

The value produced by the expression (E)0, where E is the enum identifier.

float

0.0F

int

0

long

0L

sbyte

0

short

0

struct

The value produced by setting all value-type fields to their default values and all reference-type fields to null.

uint

0

ulong

0

ushort

0

As always, feel free to comment, or ask.


Applying the same XAML style to multiple controls can be achieved by usign  derive from a base style.

<ResourceDictionary x:Class="XXX.View.Styles.TextStyle"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                    mc:Ignorable="d">
    <Style x:Key="DefaultTextStyle" TargetType="{x:Type Control}">
        <Setter Property="FontFamily" Value="Calibri"/>
        <Setter Property="FontSize" Value="14"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Margin" Value="3"/>
        <Setter Property="Height" Value="40"/>
    </Style>

    <Style x:Key="DefaultTextBoxStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource DefaultTextStyle}"/>
    <Style x:Key="DefaultLabelStyle" TargetType="{x:Type Label}" BasedOn="{StaticResource DefaultTextStyle}"/>
</ResourceDictionary>

As always, feel free to comment, or ask.


News in C# 6.0

Published 1/21/2015 by Christian

Mads Torgersen (Language PM for C# ... a danish guy - like me :-) ) gives a quick overview of what new in C# 6.0 here:

http://channel9.msdn.com/Events/Visual-Studio/Connect-event-2014/116

Nothing major, but a lot of exciting stuff. And it will certainly make my code much more ‘clean’. Looking forward starting with the 6.0 version of the greatest SW language.


Enumerate an Enum

Published 12/2/2014 by Christian in Code syntax
Tags:

Very simple but I keep forgetring:

foreach (AnEnum e in Enum.GetValues(typeof(AnEnum)))

As always, feel free to comment, or ask.


OK - So I’ve got a WPF DataGrid, with a DataGridTemplateColumn. The DataTemplate is a plane TextBox. When I tab through the cells, I want to select the text in the TextBox. And when I click the text in the TextBox, I also want to select it. That proved more difficult than I expected.

But hacking and slashing from this blog

http://andora.us/blog/2011/06/09/wpf-textbox-select-all-on-focus/

and this answer

http://stackoverflow.com/questions/1104164/wpf-datagridtemplatecolumn-am-i-missing-something

I came up with a solution. Although not strictly MVVM, I still think it falls under that design pattern. All View related stuff is still handled in the View.

Anyway - First of all. when you tab through the cells the first tab stop is the border of the cell, and the second is the actual TextBox.

Secondly, In WPF, the default behavior of the TextBox on focus is to put the cursor where it was the last time the TextBox had lost focus or if it hasn’t had focus yet, at the beginning.

The first problem I solved by by using the answer from stackoverflow:

<DataGridTemplateColumn.CellStyle>
	<Style TargetType="{x:Type DataGridCell}">
		<Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
	</Style>
</DataGridTemplateColumn.CellStyle>

The second by following Andora block. So the hole XAML DataGrid looks like this:

<DataGrid Name="TextBlockDataGrid" ItemsSource="{Binding Path=Rows}" Style="{StaticResource DefaultSettingsDataGrid}">
	<DataGrid.Columns>
		<DataGridTextColumn Binding="{Binding Text}" IsReadOnly="True"/>
		<DataGridTemplateColumn Width="*">
			<DataGridTemplateColumn.CellStyle>
				<Style TargetType="{x:Type DataGridCell}">
					<Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
				</Style>
			</DataGridTemplateColumn.CellStyle>
			<DataGridTemplateColumn.CellTemplate>
				<DataTemplate>
					<Border BorderThickness="{Binding ErrorBorderThickness}" BorderBrush="{Binding ErrorBorderBrush}">
						<TextBox Text="{Binding UserText, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" 
								 HorizontalAlignment="Right" 
								 GotKeyboardFocus="TextBox_GotKeyboardFocus" 
								 PreviewMouseDown="TextBox_PreviewMouseDown"
								 Style="{StaticResource DefaultTextBox}"/>
					</Border>
				</DataTemplate>
			</DataGridTemplateColumn.CellTemplate>
		</DataGridTemplateColumn>
	</DataGrid.Columns>
</DataGrid>

And the code-behind like this:

private void TextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
	try
	{
		((TextBox)sender).SelectAll();
	}
	catch { }
}

private void TextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
	try
	{
		// If its a triple click, select all text for the user.
		if (e.ClickCount == 3)
		{
			((TextBox)sender).SelectAll();
			return;
		}

		// Find the TextBox
		DependencyObject parent = e.OriginalSource as UIElement;
		while (parent != null && !(parent is TextBox))
		{
			parent = System.Windows.Media.VisualTreeHelper.GetParent(parent);
		}

		if (parent != null)
		{
			if (parent is TextBox)
			{
				var textBox = (TextBox)parent;
				if (!textBox.IsKeyboardFocusWithin)
				{
					// If the text box is not yet focussed, give it the focus and
					// stop further processing of this click event.
					textBox.Focus();
					e.Handled = true;
				}
			}
		}
	}
	catch { }
}

 

As always, feel free to comment, or ask.


Text - or anything else for that matter - can be scaled  to fill all available space by using a Viewbox. Here is an example.

<Viewbox HorizontalAlignment="Center" VerticalAlignment="Center">
	<TextBlock Text="{Binding Path=HeadlineText}"/>
</Viewbox>

As always, feel free to comment, or ask.


There are of course a lot of ways to collapse a XAML TextBlock, but here is one using a style.

<TextBlock Text="{Binding Path=DescriptiveText}">
	<TextBlock.Style>
		<Style TargetType="TextBlock">
			<Style.Triggers>
				<Trigger Property="Text" Value="{x:Null}">
					<Setter Property="Visibility" Value="Collapsed" />
				</Trigger>
				<Trigger Property="Text" Value="">
					<Setter Property="Visibility" Value="Collapsed" />
				</Trigger>
			</Style.Triggers>
		</Style>
	</TextBlock.Style>
</TextBlock>

As always, feel free to comment, or ask.


So now we have created an WCE7 OSDesign project. Then its time to transferre it to the device.

In short: The device boot sequence has to be interrupted. Then via a TFTP server the WCE7 image should be transferred. Here is how its done in my case.

1) First of all connect the ConnectCore i.MX53 developer kit both via ethernet and via RS232. The RS232 should be connected on the port labeled 'UART1 (CONSOLE)'

2) Start a TFTP server. OpenTFTPServer can be used here. If so just run the RunStandAloneMT.bat file.

3) Copy the WCE7 image (wce-CCXMX53) to the TFTP server root folder. In the case of OpenTFTPServer, 'C:\OpenTFTPServer' is where the wce-CCXMX53 should be placed.

4) Start a terminal application. Currently I'm useing Tera Term. Setup Tera Term with the right COM settings. Here is how the i.MX53 communicates:

5) Restart the device, and keep an eye with the terminal program. As soon as it saies 

stop autoboot. 

Enviorement variables can be viewed by typeing 'printenv'

6) Set the TFTP server ip by typeign 'set serverip' followed with to ip of the PC running the TFTP server.

7) Save the server ip, so you don't have to set it ever time you reboot the device. 'saveenv' is the command.

8) Now update the WCE7 by type 'update wce tftp'

Only the kernel partition will update. All other data will be left untouched.

And that is it.

As always, feel free to comment, or ask.


Well - at the moment I'm doing some research about WCE. Unfortunately I'm limited to use WCE7. I hope most of this goes with 'WCE8' also. Anyway ... Creating a OSDesign.

First, since its WCE7, in Visual Studio 2008, create a new project.

In the New Project dialog, select Platform Builder and OS Design

 

Click Next, and then and select the Board Support Package. In my case is a ConnectCore i.MX53

 

Click Next. All the next steps, are supplyer specific. If you want to go with the default setup, just click Finish.

 

As always, feel free to comment, or ask.

 


ICommand implementation

Published 10/31/2014 by Christian
Tags: , ,

This is a small class that implements ICommand

public class RelayCommand : ICommand
{
	#region Fields
	private readonly Action<object> execute;
	private readonly Predicate<object> canExecute;
	#endregion Fields

	#region Constructors
	public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
	{
		if (execute == null)
			throw new ArgumentNullException("execute");

		this.execute = execute;
		this.canExecute = canExecute;
	}
	#endregion Constructors

	#region ICommand Members
	[DebuggerStepThrough]
	public bool CanExecute(object parameter)
	{
		return canExecute == null ? true : canExecute(parameter);
	}

	public event EventHandler CanExecuteChanged
	{
		add { CommandManager.RequerySuggested += value; }
		remove { CommandManager.RequerySuggested -= value; }
	}

	public void Execute(object parameter)
	{
		execute(parameter);
	}
	#endregion ICommand Members
}

As always, feel free to comment, or ask.