SolidColorBrush Opacity property from 1 to 0


   
 
<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">

    <StackPanel>
        <Rectangle Width="100" Height="50" Stroke="Black" StrokeThickness="1" >
          <Rectangle.Fill>
            <SolidColorBrush Color="Blue" Opacity="1" />
          </Rectangle.Fill>
        </Rectangle>
        <Rectangle Width="100" Height="50" Stroke="Black" StrokeThickness="1">
          <Rectangle.Fill>
            <SolidColorBrush Color="Blue" Opacity="0.75" />
          </Rectangle.Fill>
        </Rectangle>
        <Rectangle Width="100" Height="50" Stroke="Black" StrokeThickness="1">
          <Rectangle.Fill>
            <SolidColorBrush Color="Blue" Opacity="0.5" />
          </Rectangle.Fill>
        </Rectangle>
        <Rectangle Width="100" Height="50" Stroke="Black" StrokeThickness="1">
          <Rectangle.Fill>
            <SolidColorBrush Color="Blue" Opacity="0.25" />
          </Rectangle.Fill>
        </Rectangle>
        <Rectangle Width="100" Height="50" Stroke="Black" StrokeThickness="1">
          <Rectangle.Fill>
            <SolidColorBrush Color="Blue" Opacity="0" />
          </Rectangle.Fill>
        </Rectangle>
    </StackPanel>
</Window>

   
     


The Opacity property of this rectangle's SolidColorBrush is animated


   
 

<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">
   <StackPanel>
      <Rectangle 
        Width="100" Height="50" Stroke="Black" StrokeThickness="1"
        Grid.Column="2" Grid.Row="1"
        HorizontalAlignment="Center" Margin="10">
        <Rectangle.Fill>
          <SolidColorBrush x:Name="solidColorBrush2" Color="Blue" />
        </Rectangle.Fill>
      </Rectangle>

      <Button Grid.Column="2" Grid.Row="2"
        HorizontalAlignment="Center">
        Start Animation
        <Button.Triggers>
          <EventTrigger RoutedEvent="Button.Click">
            <BeginStoryboard>
              <Storyboard>
                
                <!-- Animates the opacity of a SolidColorBrush from 1 to 0 over 10 seconds. -->
                <DoubleAnimation Storyboard.TargetName="solidColorBrush2"
                  Storyboard.TargetProperty="Opacity"
                  From="1.0" To="0.0" Duration="0:0:10" />
              </Storyboard>
            </BeginStoryboard>
          </EventTrigger>
        </Button.Triggers>
      </Button>
   
   
   </StackPanel>
</Window>

   
     


The Color property of the SolidColorBrush used to fill this rectangle is animated


   
 

<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">
   <StackPanel>
      <Rectangle 
        Width="100" Height="50" Stroke="Black" StrokeThickness="1"
        Margin="10" HorizontalAlignment="Center"
        Grid.Column="0" Grid.Row="1">
        <Rectangle.Fill>
          <SolidColorBrush x:Name="solidColorBrush1" Color="Blue" />
        </Rectangle.Fill>
      </Rectangle>

      <Button Grid.Column="0" Grid.Row="2"
        HorizontalAlignment="Center">
        Start Animation
        <Button.Triggers>
          <EventTrigger RoutedEvent="Button.Click">
            <BeginStoryboard>
              <Storyboard>
                <ColorAnimation Storyboard.TargetName="solidColorBrush1"
                  Storyboard.TargetProperty="Color"
                  From="Blue" To="Red" Duration="0:0:10" />
              </Storyboard>
            </BeginStoryboard>
          </EventTrigger>
        </Button.Triggers>
      </Button>

   
   </StackPanel>
</Window>

   
     


The Color property of this rectangle's SolidColorBrush is animated.


   
 

<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">
    <StackPanel>
      <Rectangle Width="100" Height="50" Stroke="Black" StrokeThickness="1"
        Grid.Column="4" Grid.Row="1"
        HorizontalAlignment="Center" Margin="10">
        <Rectangle.Fill>
          <SolidColorBrush x:Name="solidColorBrush3" Color="Blue" />
        </Rectangle.Fill>
      </Rectangle>

      <Button Grid.Column="4" Grid.Row="2"
        HorizontalAlignment="Center">
        Start Animation
        <Button.Triggers>
          <EventTrigger RoutedEvent="Button.Click">
            <BeginStoryboard>
              <Storyboard>
                <ColorAnimation Storyboard.TargetName="solidColorBrush3"
                  Storyboard.TargetProperty="Color"
                  From="Blue" To="#00FF0000" Duration="0:0:10" />
              </Storyboard>
            </BeginStoryboard>
          </EventTrigger>
        </Button.Triggers>
      </Button>
   </StackPanel>
</Window>

   
     


slider value changed event


   
  

<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" Height="200" Width="300">
    <StackPanel>
        <TextBlock Margin="5" Text="0" FontSize="20" 
                   HorizontalAlignment="Center" Name="txtSliderValue" />
        <Slider LargeChange="10" Margin="5" Maximum="1000" Minimum="0" 
                Name="slider1" TickPlacement="TopLeft" 
                Ticks="100, 200, 400, 800" Value="0"
                ValueChanged="slider_ValueChanged" />
        <Button Name="btnGetSliderValue1" Width="100" 
                Click="GetSliderValue_Click">Get Slider 1 Value</Button>
        <Slider IsSnapToTickEnabled="True" Margin="5" Maximum="1000" 
                Minimum="0" Name="slider2" TickFrequency="25" 
                TickPlacement="BottomRight" Value="1000" 
                ValueChanged="slider_ValueChanged" />
        <Button Name="btnGetSliderValue2" Width="100" 
                Click="GetSliderValue_Click">Get Slider 2 Value</Button>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void GetSliderValue_Click(object sender, RoutedEventArgs e)
        {
            Button button = e.OriginalSource as Button;
            string message = "Unknown slider.";

            if (button == btnGetSliderValue1)
            {
                message = "Slider1 value = " + slider1.Value;
            }
            else if (button == btnGetSliderValue2)
            {
                message = "Slider2 value = " + slider2.Value;
            }

            MessageBox.Show(message, Title);
        }
        private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            Slider slider = e.OriginalSource as Slider;

            if (slider != null)
            {
                txtSliderValue.Text = slider.Value.ToString();
            }
        }
    }
}

   
    
     


Get User Input from a Slider


   
  
<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" Height="200" Width="300">
    <StackPanel>
        <TextBlock Margin="5" Text="0" FontSize="20" 
                   HorizontalAlignment="Center" Name="txtSliderValue" />
        <Slider LargeChange="10" Margin="5" Maximum="1000" Minimum="0" 
                Name="slider1" TickPlacement="TopLeft" 
                Ticks="100, 200, 400, 800" Value="0"
                ValueChanged="slider_ValueChanged" />
        <Button Name="btnGetSliderValue1" Width="100" 
                Click="GetSliderValue_Click">Get Slider 1 Value</Button>
        <Slider IsSnapToTickEnabled="True" Margin="5" Maximum="1000" 
                Minimum="0" Name="slider2" TickFrequency="25" 
                TickPlacement="BottomRight" Value="1000" 
                ValueChanged="slider_ValueChanged" />
        <Button Name="btnGetSliderValue2" Width="100" 
                Click="GetSliderValue_Click">Get Slider 2 Value</Button>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void GetSliderValue_Click(object sender, RoutedEventArgs e)
        {
            Button button = e.OriginalSource as Button;
            string message = "Unknown slider.";

            if (button == btnGetSliderValue1)
            {
                message = "Slider1 value = " + slider1.Value;
            }
            else if (button == btnGetSliderValue2)
            {
                message = "Slider2 value = " + slider2.Value;
            }

            MessageBox.Show(message, Title);
        }
        private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            Slider slider = e.OriginalSource as Slider;

            if (slider != null)
            {
                txtSliderValue.Text = slider.Value.ToString();
            }
        }
    }
}

   
    
     


Slider Attributes


   
  

<Window x:Class="ControlDemos.Label"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ControlDemos" Height="300" Width="400">
    <Grid>
      <Slider Name="sl1" Minimum="0" Maximum="100" Value="50" IsSnapToTickEnabled="True" 
      TickPlacement="BottomRight" TickFrequency="5" IsSelectionRangeEnabled="True" 
      SelectionStart="60" SelectionEnd="90" ValueChanged="sl1_ValueChanged"/>
  </Grid>
</Window>


//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Input;
namespace ControlDemos
{

    public partial class Label : Window
    {

        public Label()
        {
            InitializeComponent();
        }
        private void sl1_ValueChanged(object sender, EventArgs e)
        {
            if (sl1.Value < sl1.SelectionStart)
                sl1.Value = sl1.SelectionStart;

            if (sl1.Value > sl1.SelectionEnd)
                sl1.Value = sl1.SelectionEnd;
        }

    }
}