Horizontal Linear Gradient and GradientStop


   
     

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Microsoft.Samples.Graphics.RectangleExample"
    WindowTitle="Example">
  <Canvas>

    <Rectangle Width="150" Height="100" Grid.Row="2" Margin="5">
      <Rectangle.Fill>
        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
          <GradientStop Color="Blue" Offset="0"/>
          <GradientStop Color="White" Offset="1" />
        </LinearGradientBrush>
      </Rectangle.Fill>
    </Rectangle>
    <TextBlock Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" Margin="5">Horizontal Linear Gradient</TextBlock>

  </Canvas>
</Page>

   
    
    
    
    
     


Change Fill


   
   

<StackPanel 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/interactivedesigner/2006"
  mc:Ignorable="d"
  x:Class="PaintDrawExamples.ChangeFill" 
  Width="640" Height="480">

  <StackPanel.Resources>
    <Storyboard x:Key="OnLoaded"/>
  </StackPanel.Resources>

  <StackPanel.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
      <BeginStoryboard x:Name="OnLoaded_BeginStoryboard" Storyboard="{DynamicResource OnLoaded}"/>
    </EventTrigger>
  </StackPanel.Triggers>
  <Path d:LastTangent="0,0" Width="300" Height="82" x:Name="myShape" RenderTransformOrigin="0.5,0.5" Stretch="Fill" Data="M268,209 L134,209 147,249 250,290 z">
    <Path.RenderTransform>
      <TransformGroup>
        <TranslateTransform X="0" Y="0"/>
        <ScaleTransform ScaleX="1" ScaleY="1"/>
        <SkewTransform AngleX="0" AngleY="0"/>
        <RotateTransform Angle="0"/>
        <TranslateTransform X="0" Y="0"/>
      </TransformGroup>
    </Path.RenderTransform>
    <Path.Fill>
      <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
        <GradientStop Color="#11111111" Offset="0"/>
        <GradientStop Color="#FFFFFFFF" Offset="1"/>
      </LinearGradientBrush>
    </Path.Fill>
  </Path>
</StackPanel>
//File:Window.xaml.cs

using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

namespace PaintDrawExamples
{
  public partial class ChangeFill
  {
    public ChangeFill()
    {
      this.InitializeComponent();
      this.MouseDown += new MouseButtonEventHandler(MouseDownHandler);
    }

    private void MouseDownHandler(object sender, MouseButtonEventArgs e)
    {
      LinearGradientBrush brush = (LinearGradientBrush)this.myShape.Fill;
      brush.GradientStops[0].Color = Color.FromRgb(0,0,0);
    }
  }
}

   
    
    
     


Update Focused Field for FrameworkElement

   
 

//http://simpledbbrowser.codeplex.com/
//License:  Microsoft Public License (Ms-PL)  
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace AWS.Framework.WPF.Utility
{
    public sealed class Helpers
    {
        #region Constructor

        private Helpers()
        { }

        #endregion // Constructor

        #region Methods


        public static void UpdateFocusedField()
        {
            FrameworkElement fwe = Keyboard.FocusedElement as FrameworkElement;

            if (fwe != null)
            {
                BindingExpression expression = null;

                if (fwe is TextBox)
                    expression = fwe.GetBindingExpression(TextBox.TextProperty);
                // TODO - developers - add more control types here. Won&#039;t be that many.
                // this would include custom TextBox controls or 3rd party TextBox controls

                if (expression != null)
                    expression.UpdateSource();
            }
        }

        #endregion // Methods
    }
}

   
     


Find Visual Child

   
 
//http://simpledbbrowser.codeplex.com/
//License:  Microsoft Public License (Ms-PL)  
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace AWS.Framework.WPF.Utility
{
    public sealed class Helpers
    {
        public static childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
        {
            for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);

                if (child != null &amp;&amp; child is childItem)
                    return (childItem)child;
                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);

                    if (childOfChild != null)
                        return childOfChild;
                }
            }

            return null;
        }
   }
}

   
     


The UniformGrid


   
     

<Window x:Class="LayoutPanels.TheUniformGrid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TheUniformGrid" Height="300" Width="300">
    <UniformGrid Rows="2" Columns="2">
      <Button>Top Left</Button>
      <Button>Top Right</Button>
      <Button>Bottom Left</Button>
      <Button>Bottom Right</Button>
    </UniformGrid>
</Window>

   
    
    
    
    
     


Implement validation logic on custom objects and then bind to them(Mouse-over to see the validation error message).


   
  


<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WPF IDataErrorInfo Sample" Width="350" Height="150"
        xmlns:src="clr-namespace:WpfApplication1">
    
    <Window.Resources>
        <src:Employee x:Key="data"/>
        <Style x:Key="textBoxInError" TargetType="TextBox">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip"
                            Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                        Path=(Validation.Errors)&#91;0&#93;.ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <StackPanel Margin="20">
        <TextBox Style="{StaticResource textBoxInError}">
            <TextBox.Text>
                <Binding Path="Age" Source="{StaticResource data}"
                         ValidatesOnExceptions="True"
                         UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <DataErrorValidationRule/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
    </StackPanel>
</Window>
//File:Window.xaml.cs
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.ComponentModel;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }
    public class Employee : IDataErrorInfo
    {
        private int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }

        public string Error
        {
            get
            {
                return null;
            }
        }

        public string this[string name]
        {
            get
            {
                string result = null;

                if (name == "Age")
                {
                    if (this.age < 0 || this.age > 50)
                    {
                        result = "Age must not be less than 0 or greater than 50.";
                    }
                }
                return result;
            }
        }
    }
}

   
    
     


IValueConverter and ValidationRule


   
  
<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:sys="clr-namespace:System;assembly=mscorlib"
  xmlns:local="clr-namespace:WpfApplication1" 
  Title="Random Numbers Built to Order">

  <Window.Resources>
    <ObjectDataProvider x:Key="nextRandomNumber" ObjectType="{x:Type sys:Random}"  MethodName="Next">
      <ObjectDataProvider.MethodParameters>
        <sys:Int32>0</sys:Int32>
        <sys:Int32>10</sys:Int32>
      </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
    <local:StringToInt32Converter x:Key="numConverter" />
  </Window.Resources>

  <StackPanel DataContext="{StaticResource nextRandomNumber}">
    <Label>Min</Label>
    <TextBox Name="minTextBox" ToolTip="{Binding ElementName=minTextBox, Path=(Validation.Errors)&#91;0&#93;.ErrorContent}">
      <TextBox.Text>
        <Binding Path="MethodParameters&#91;0&#93;" BindsDirectlyToSource="True" Converter="{StaticResource numConverter}">
          <Binding.ValidationRules>
            <local:IntegerValidator />
          </Binding.ValidationRules>
        </Binding>
      </TextBox.Text>
    </TextBox>
    <Label>Max</Label>
    <TextBox Name="maxTextBox" ToolTip="{Binding ElementName=maxTextBox, Path=(Validation.Errors)&#91;0&#93;.ErrorContent}">
      <TextBox.Text>
        <Binding Path="MethodParameters&#91;1&#93;" BindsDirectlyToSource="True" Converter="{StaticResource numConverter}">
          <Binding.ValidationRules>
            <local:IntegerValidator />
          </Binding.ValidationRules>
        </Binding>
      </TextBox.Text>
    </TextBox>
    <Label>Random</Label>
    <TextBox Text="{Binding Mode=OneTime}" IsReadOnly="True" />
    <Button Name="nextButton">Next</Button>
  </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Collections.Generic;
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.Shapes;

namespace WpfApplication1 {
  public partial class Window1 : System.Windows.Window {

    public Window1() {
      InitializeComponent();
      nextButton.Click += new RoutedEventHandler(nextButton_Click);
    }

    void nextButton_Click(object sender, RoutedEventArgs e) {
      ((ObjectDataProvider)Resources["nextRandomNumber"]).Refresh();
    }

  }

  public class StringToInt32Converter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      return ((int)value).ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      return int.Parse((string)value);
    }
  }

  public class IntegerValidator : ValidationRule {
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) {
      try {
        int.Parse((string)value);
        return ValidationResult.ValidResult;
      }
      catch( Exception ex ) {
        return new ValidationResult(false, ex.Message);
      }
    }
  }
}