Monday, December 14, 2009

Five Minute Silverlight 4 Aides-Memoire #7 – IDataErrorInfo

The XAML:

DATA INPUT
  1. <Grid x:Name="LayoutRoot">
  2.   <Grid.Resources>
  3.     <local:Person x:Key="NewJoiner" Age="49" Forename="Fred" Surname="Flintstone" />
  4.   </Grid.Resources>
  5.   <Grid DataContext="{StaticResource NewJoiner}">
  6.     <Grid.ColumnDefinitions>
  7.       <columndefinition Width="1*" />
  8.       <columndefinition Width="5*" />
  9.     </Grid.ColumnDefinitions>
  10.     <Grid.RowDefinitions>
  11.       <rowdefinition Height="50" />
  12.       <rowdefinition Height="50" />
  13.       <rowdefinition Height="50" />
  14.     </Grid.RowDefinitions>
  15.     <Grid.Resources>
  16.       <Style TargetType="TextBlock">
  17.         <setter Property="Margin" Value="10" />
  18.         <setter Property="HorizontalAlignment" Value="Right" />
  19.         <setter Property="VerticalAlignment" Value="Center" />
  20.       </Style>
  21.       <Style TargetType="TextBox">
  22.         <setter Property="Margin" Value="10" />
  23.         <setter Property="Width" Value="154" />
  24.         <setter Property="HorizontalAlignment" Value="Left" />
  25.         <setter Property="VerticalAlignment" Value="Center" />
  26.       </Style>
  27.     </Grid.Resources>
  28.     <TextBlock Text="First Name" Grid.Row="0" Grid.Column="0" />
  29.     <TextBlock Text="Last Name" Grid.Row="1" Grid.Column="0" />
  30.     <TextBlock Text="Age" Grid.Row="2" Grid.Column="0" />
  31.     <textbox x:Name="Forename" Grid.Row="0" Grid.Column="1" Text="{Binding Forename, Mode=TwoWay, ValidatesOnDataErrors=True}" />
  32.     <textbox x:Name="Surname" Grid.Row="1" Grid.Column="1" Text="{Binding Surname, Mode=TwoWay, ValidatesOnDataErrors=True}" />
  33.     <textbox x:Name="Age" Grid.Row="2" Grid.Column="1" Text="{Binding Age, Mode=TwoWay, ValidatesOnDataErrors=True}" />
  34.   </Grid>
  35. </Grid>

The code:

IMPLEMENTATION
  1. public class Person : IDataErrorInfo
  2. {
  3.     public string Forename { get; set; }
  4.     public string Surname { get; set; }
  5.     public int Age { get; set; }
  6.     public string Error { get { return null; } }
  7.  
  8.     public string this[string columnName]
  9.     {
  10.         get
  11.         {
  12.             string error = null;
  13.             switch (columnName)
  14.             {
  15.                 case "Forename":
  16.                     if (string.IsNullOrEmpty(Forename))
  17.                         error = "Forename required";
  18.                     break;
  19.                 case "Surname":
  20.                     if (string.IsNullOrEmpty(this.Surname))
  21.                         error = "Surname required";
  22.                     break;
  23.                 case "Age":
  24.                     if (this.Age < 0 || this.Age > 130)
  25.                         error = "Invalid Age";
  26.                     break;
  27.             }
  28.             return error;
  29.         }
  30.     }
  31. }


The result:

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;
}