Monday, December 14, 2009

Five Minute Silverlight 4 Aides-Memoire #7 – IDataErrorInfo

<Grid x:Name="LayoutRoot">
<
Grid.Resources>
<
local:Person x:Key="NewJoiner" Age="49" Forename="Fred" Surname="Flintstone" />
</
Grid.Resources>
<
Grid DataContext="{StaticResource NewJoiner}">
<
Grid.ColumnDefinitions>
<
ColumnDefinition Width="1*" />
<
ColumnDefinition Width="5*" />
</
Grid.ColumnDefinitions>
<
Grid.RowDefinitions>
<
RowDefinition Height="50" />
<
RowDefinition Height="50" />
<
RowDefinition Height="50" />
</
Grid.RowDefinitions>
<
Grid.Resources>
<
Style TargetType="TextBlock">
<
Setter Property="Margin" Value="10" />
<
Setter Property="HorizontalAlignment" Value="Right" />
<
Setter Property="VerticalAlignment" Value="Center" />
</
Style>
<
Style TargetType="TextBox">
<
Setter Property="Margin" Value="10" />
<
Setter Property="Width" Value="154" />
<
Setter Property="HorizontalAlignment" Value="Left" />
<
Setter Property="VerticalAlignment" Value="Center" />
</
Style>
</
Grid.Resources>
<
TextBlock Text="First Name" Grid.Row="0" Grid.Column="0" />
<
TextBlock Text="Last Name" Grid.Row="1" Grid.Column="0" />
<
TextBlock Text="Age" Grid.Row="2" Grid.Column="0" />
<
TextBox x:Name="Forename" Grid.Row="0" Grid.Column="1" Text="{Binding Forename, Mode=TwoWay, ValidatesOnDataErrors=True}" />
<
TextBox x:Name="Surname" Grid.Row="1" Grid.Column="1" Text="{Binding Surname, Mode=TwoWay, ValidatesOnDataErrors=True}" />
<
TextBox x:Name="Age" Grid.Row="2" Grid.Column="1" Text="{Binding Age, Mode=TwoWay, ValidatesOnDataErrors=True}" />
</
Grid>
</
Grid>


public class Person : IDataErrorInfo
{
public string Forename { get; set; }
public string Surname { get; set; }
public int Age { get; set; }

public string Error { get { return null; } }

public string this[string columnName]
{
get
{
string error = null;
switch (columnName)
{
case "Forename":
if (string.IsNullOrEmpty(this.Forename))
error = "Forename required";
break;
case "Surname":
if (string.IsNullOrEmpty(this.Surname))
error = "Surname required";
break;
case "Age":
if (this.Age < 0 || this.Age > 130)
error = "Invalid Age";
break;
}
return error;
}
}
}



image 

Five Minute Silverlight 4 Aides-Memoire #6 – Right Mouse Event Support

<Popup x:Name="PopupMenu" IsOpen="False">
    <ListBox x:Name="MenuItems">
        <ListBoxItem Content="Item 1" />
        <ListBoxItem Content="Item 2" />
        <ListBoxItem Content="Item 3" />
        <ListBoxItem Content="Item 4" />
    </ListBox>
</Popup>


public MainPage()
{
    InitializeComponent();
    MouseRightButtonDown += (s, e) => e.Handled = true;
    MouseRightButtonUp += MainPage_MouseRightButtonUp;
    MenuItems.MouseLeftButtonUp += MenuItems_MouseLeftButtonUp;
}


void MainPage_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    this.PopupMenu.HorizontalOffset = e.GetPosition(this).X;
    this.PopupMenu.VerticalOffset = e.GetPosition(this).Y;
    this.PopupMenu.IsOpen = true;
}

void MenuItems_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    PopupMenu.IsOpen = false;
    System.Diagnostics.Debug.WriteLine(((sender as ListBox).SelectedItem as ListBoxItem).Content);
}

Five Minute Silverlight 4 Aides-Memoire #5 – Rich Text Area Control

<RichTextArea x:Name="Editor">
    <RichTextArea.Blocks>
        <Paragraph>
            <Run Foreground="Blue" Text="This is "/>
            <Run Foreground="Red" FontWeight="Bold" Text="some sample " />
            <Run FontStyle="Italic" Text="text" />
        </Paragraph>
        <Paragraph>
            <Run Text="Here is "/>
            <Run TextDecorations="Underline" Text="some more" />
        </Paragraph>
    </RichTextArea.Blocks>
</RichTextArea>

void UnderlineSelection_Click(object sender, RoutedEventArgs e)
{
    var currentValue = this.Editor.Selection.GetPropertyValue(TextElement.TextDecorationsProperty);
    TextDecorationCollection setValue = TextDecorations.Underline;
    if (currentValue != DependencyProperty.UnsetValue)
        setValue = (TextDecorationCollection)currentValue == setValue ? null : setValue;
    this.Editor.Selection.SetPropertyValue(TextElement.TextDecorationsProperty, setValue);
}

void ItalicSelection_Click(object sender, RoutedEventArgs e)
{
    var currentValue = this.Editor.Selection.GetPropertyValue(TextElement.FontStyleProperty);
    FontStyle setValue = FontStyles.Italic;
    FontStyle defaultValue = FontStyles.Normal;
    if (currentValue != DependencyProperty.UnsetValue)
        setValue = (FontStyle)currentValue == setValue ? defaultValue : setValue;
    this.Editor.Selection.SetPropertyValue(TextElement.FontStyleProperty, setValue);
}

void BoldSelection_Click(object sender, RoutedEventArgs e)
{
    var currentValue = this.Editor.Selection.GetPropertyValue(TextElement.FontWeightProperty);
    FontWeight setValue = FontWeights.Bold;
    FontWeight defaultValue = FontWeights.Normal;
    if (currentValue != DependencyProperty.UnsetValue)
        setValue = (FontWeight)currentValue == setValue ? defaultValue : setValue;
    this.Editor.Selection.SetPropertyValue(TextElement.FontWeightProperty, setValue);
}

image

Friday, December 04, 2009

Five Minute Silverlight 4 Aides-Memoire #4 – Clipboard Access

private void PopButton_Click(object sender, RoutedEventArgs e)
{
    var newPara = new Paragraph();

    newPara.Inlines.Add(new Run { Text = Clipboard.GetText() });

    this.rightRichTextArea.Blocks.Add(newPara);
}

private void PushButton_Click(object sender, RoutedEventArgs e)
{
    Clipboard.SetText(this.leftRichTextArea.Selection.Text);
}

Tuesday, December 01, 2009

Five Minute Silverlight 4 Aides-Memoire #3 – WebCam/Microphone Device Support

<UserControl.Resources>
<
Style x:Key="ListBoxStyle" TargetType="ListBox">
<
Setter Property="ItemTemplate">
<
Setter.Value>
<
DataTemplate>
<
Image Margin="5" Source="{Binding}" Stretch="UniformToFill" Height="80" VerticalAlignment="Center"/>
</
DataTemplate>
</
Setter.Value>
</
Setter>
<
Setter Property="ItemsPanel">
<
Setter.Value>
<
ItemsPanelTemplate>
<
StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</
ItemsPanelTemplate>
</
Setter.Value>
</
Setter>
</
Style>
</
UserControl.Resources>



<StackPanel Grid.Row="2" Grid.Column="0">
<
Button Content="Start WebCam" Click="StartWebCam_Click" Height="30" />
<
Button Content="Snapshot" Click="Snapshot_Click" Height="30" />
</
StackPanel>
<
Rectangle Grid.Row="0" Stretch="Fill" Grid.Column="1" x:Name="CapturedImage" />
<ListBox Grid.Row="1" Margin="10" Grid.RowSpan="2" Grid.Column="1" x:Name="Snapshots" Style="{StaticResource ListBoxStyle}"/>


private void StartWebCam_Click(object sender, RoutedEventArgs e)
{
if (!CaptureDeviceConfiguration.AllowedDeviceAccess & !CaptureDeviceConfiguration.RequestDeviceAccess())
return;

VideoBrush videoBrush = new VideoBrush();
videoBrush.SetSource(captureSource);
CapturedImage.Fill = videoBrush;
this.Snapshots.ItemsSource = snaps;
captureSource.Start();
}


private void Snapshot_Click(object sender, RoutedEventArgs e)
{
captureSource.AsyncCaptureImage((image) => { snaps.Add(image); });
}

Five Minute Silverlight 4 Aides-Memoire #2 – Printing

private void Print_Click(object sender, RoutedEventArgs e)
{
    PrintDocument printDoc = new PrintDocument();

    // Name that will show in the spooler...
    printDoc.DocumentName = "Directory Tree";
    printDoc.PrintPage += new EventHandler<PrintPageEventArgs>(printDoc_PrintPage);
    printDoc.Print();
}

private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
{
    // Needs a UIElement (visual tree)
    e.PageVisual = DirectoriesTreeView;
    // Setting true will ensure PrintPage is called again for subsequent pages
    e.HasMorePages = false;
}

Monday, November 30, 2009

Five Minute Silverlight 4 Aides-Memoire #1 – Drag and Drop

Build a hierarchical treeview of files and directories dropped from a Windows Explorer window. Requires elevated trust running out of browser to avoid security exceptions when accessing the directory information in the FileInfo objects returned from the e.Data.GetData call.

image



<Grid x:Name="LayoutRoot" AllowDrop="True" Drop="LayoutRoot_Drop">
<
my:TreeView x:Name="DirectoriesTreeView" Margin="50" Width="500" />
</
Grid>

private void LayoutRoot_Drop(object sender, DragEventArgs e)
{
if (!Application.Current.HasElevatedPermissions e.Data == null)
return;

FileInfo[] fileInfos = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];
if (fileInfos == null)
return;
List<FileInfo> sortedList = new List<FileInfo>(fileInfos);

sortedList.Sort((x, y) =>
{
if (x.Attributes == FileAttributes.Directory && y.Attributes != FileAttributes.Directory)
return -1;
if (y.Attributes == FileAttributes.Directory && x.Attributes != FileAttributes.Directory)
return 1;
return x.Name.CompareTo(y.Name);
});

foreach (FileInfo fileInfo in sortedList)
{
var newNode = new TreeViewItem { Header = fileInfo.Name };
DirectoriesTreeView.Items.Add(newNode);
if (!fileInfo.Exists)
ProcessDirectory(fileInfo, newNode);
}
}
private static void ProcessDirectory(FileSystemInfo fileSystemInfo, TreeViewItem currentNode)
{
DirectoryInfo directoryInfo = new DirectoryInfo(fileSystemInfo.FullName);

foreach (FileSystemInfo childFileSystemInfo in directoryInfo.EnumerateFileSystemInfos())
{
var newNode = new TreeViewItem { Header = childFileSystemInfo.Name };
currentNode.Items.Add(newNode);
if (childFileSystemInfo.Attributes == FileAttributes.Directory)
ProcessDirectory(childFileSystemInfo, newNode);
}
}

Wednesday, November 18, 2009

Silverlight 4

Quite unexpectedly, at least I wasn’t expecting it, today saw the announcement of the first beta of Silverlight 4 at PDC09 in Los Angeles. Barely has version 3 made it out there, but we have a new beta to play with together with another rev of Blend to support it (and .NET 4 in general).
A quick run through the main features in the beta:
  • Printing – Full access to the regular print facilities of the host machine – the print dialog and print preview features – so long to all the writable bitmaps and HTML bridge hacks
  • Webcam/microphone support – Subject to UAC/user confirmation, you can now programmatically get access to connected devices, capture video, stills and audio input streams
  • A Rich TextBox control – the WPF RTF control makes its appearance in Silverlight – embed other UI Elements, control text Runs and other formatting such as Text Decorations
  • Clipboard Access – Programmatic access to the clipboard – again subject to UAC
  • Right Mouse Context Menus – Full control of a customisable context menu – add whatever you want to it and respond to selection events
  • Drag-Drop Drop-Target support Silverlight apps can now act as drag-drop drop-targets receiving file information for the dropped file object(s)
  • MVVM Commanding – Finally proper implementation of the ICommand interface – no longer a need to kludge it via bindings to helper classes
  • IDataErrorInfo/Validation support – Integration of the support for the IDataErrorInfo interface in Silverlight’s DataBinding giving a validation framework for input controls to leverage in consistently surfacing data input errors with asynchronous data validation
  • String Formatting in Binding Extensions – As with WPF, Silverlight has been retrofitted with improved formatting support in the binding extension to give better control over stuff like date, time and other formatting for locales and long and short formatting
  • Out of Browser improvements: HTML content - Hosting of HTML content within the Silverlight app; Notifications (aka toast) support; Elevated Trust (again via UAC) access to the My Documents, My Videos, My Pictures special folders; Cross domain access and Full keyboard access in Full Screen mode
  • Better tooling and integration of RIA/ADO Services/WCF/MEF
Lots to check out and try out – exciting stuff!

Tuesday, August 11, 2009

Five Minute Silverlight 3 Aides-Memoire #5 – Based on Styles

    <UserControl.Resources>
<
Style x:Key="ColouredContentControlStyle" TargetType="ContentControl">
<
Setter Property="Height" Value="30" />
<
Setter Property="Width" Value="100" />
<
Setter Property="Margin" Value="10" />
<
Setter Property="FontFamily" Value="Verdana" />
<
Setter Property="FontSize" Value="14" />
<
Setter Property="FontStyle" Value="Italic" />
</
Style>

<
Style x:Key="ColouredCheckBoxStyle" BasedOn="{StaticResource ColouredContentControlStyle}" TargetType="CheckBox">
<
Setter Property="Width" Value="154" />
</
Style>

<
Style x:Key="RedButtonStyle" BasedOn="{StaticResource ColouredContentControlStyle}" TargetType="Button">
<
Setter Property="Foreground" Value="Red" />
</
Style>
<
Style x:Key="GreenButtonStyle" BasedOn="{StaticResource ColouredContentControlStyle}" TargetType="Button">
<
Setter Property="Foreground" Value="Green" />
</
Style>
<
Style x:Key="YellowCheckBoxStyle" BasedOn="{StaticResource ColouredCheckBoxStyle}" TargetType="CheckBox">
<
Setter Property="Foreground" Value="Yellow" />
</
Style>
<
Style x:Key="OrangeRadioButtonStyle" BasedOn="{StaticResource ColouredContentControlStyle}" TargetType="RadioButton">
<
Setter Property="Foreground" Value="Orange" />
<
Setter Property="Width" Value="190" />
<
Setter Property="FontWeight" Value="Bold" />
</
Style>
</
UserControl.Resources>
<
Grid x:Name="LayoutRoot">
<
Grid.Background>
<
LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<
GradientStop Color="Black" Offset="0"/>
<
GradientStop Color="White" Offset="1"/>
</
LinearGradientBrush>
</
Grid.Background>
<
StackPanel Orientation="Vertical" Margin="20">
<
Button Style="{StaticResource RedButtonStyle}" Content="Red Button" />
<
CheckBox Style="{StaticResource YellowCheckBoxStyle}" Content="Yellow CheckBox" />
<
RadioButton Style="{StaticResource OrangeRadioButtonStyle}" Content="Orange RadioButton" />
<
Button Style="{StaticResource GreenButtonStyle}" Content="Green Button" />
</
StackPanel>
</
Grid>


FiveMinuteSilverlight3_5

Five Minute Silverlight 3 Aides-Memoire #4 – Merged Resource Dictionaries

ButtonStyles.xaml:

<ResourceDictionary 
x:Name="ButtonsStyles"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<
Style x:Key="RedButton" TargetType="Button">
<
Setter Property="Foreground" Value="Red" />
<
Setter Property="Height" Value="30" />
<
Setter Property="Width" Value="100" />
<
Setter Property="Margin" Value="10" />
</
Style>
<
Style x:Key="GreenButton" TargetType="Button">
<
Setter Property="Foreground" Value="Green" />
<
Setter Property="Height" Value="30" />
<
Setter Property="Width" Value="100" />
<
Setter Property="Margin" Value="10" />
</
Style>
</
ResourceDictionary>


CheckBoxStyles.xaml:




<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="CheckBoxStyles"
>
<
Style x:Key="YellowCheckBox" TargetType="CheckBox">
<
Setter Property="Foreground" Value="Yellow" />
<
Setter Property="Height" Value="30" />
<
Setter Property="Margin" Value="10" />
</
Style>
<
Style x:Key="OrangeCheckBox" TargetType="CheckBox">
<
Setter Property="Foreground" Value="Orange" />
<
Setter Property="Height" Value="30" />
<
Setter Property="Margin" Value="10" />
</
Style>
</
ResourceDictionary>


MainPage.xaml:




<UserControl.Resources>
<
ResourceDictionary>
<
ResourceDictionary.MergedDictionaries>
<
ResourceDictionary Source="ButtonStyles.xaml" />
<
ResourceDictionary Source="CheckBoxStyles.xaml" />
</
ResourceDictionary.MergedDictionaries>
</
ResourceDictionary>
</
UserControl.Resources>
<
Grid x:Name="LayoutRoot">
<
Grid.Background>
<
LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<
GradientStop Color="Black" Offset="0"/>
<
GradientStop Color="White" Offset="1"/>
</
LinearGradientBrush>
</
Grid.Background>
<
StackPanel Orientation="Horizontal">
<
Button Grid.Row="0" Grid.Column="0" Style="{StaticResource RedButton}" Content="Red Button" />
<
Button Grid.Row="0" Grid.Column="1" Style="{StaticResource GreenButton}" Content="Green Button" />
<
CheckBox Grid.Row="1" Grid.Column="0" Style="{StaticResource YellowCheckBox}" Content="Yellow CheckBox" />
<
CheckBox Grid.Row="1" Grid.Column="1" Style="{StaticResource OrangeCheckBox}" Content="Orange CheckBox" />
</
StackPanel>
</
Grid>

FiveMinuteSilverlight3_4