Silverlight Opacity with Slider


Get Microsoft Silverlight

XAML

<UserControl x:Class="LAOpacityMask2.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid x:Name="LayoutRoot" Background="White">

        <StackPanel Background="White" Orientation="Vertical">
            
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="600"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="400"></RowDefinition>
                    <RowDefinition Height="50"></RowDefinition>
                </Grid.RowDefinitions>
                
                <Image Source="snow.jpg" Grid.Column="1" Grid.Row="0" Margin="5"></Image>
                <Image Source="desert.jpg" Grid.Column="1" Grid.Row="0" Margin="5">
                    <Image.OpacityMask>
                        <LinearGradientBrush StartPoint="0.0, 0.5" EndPoint="1.0, 0.5">
                            <GradientStop Color="#FF000000" Offset="0.0"></GradientStop>
                            <GradientStop x:Name="Divider" Color="#AE000000" Offset="0.5"></GradientStop>
                            <GradientStop Color="#00000000" Offset="1.0"></GradientStop>
                        </LinearGradientBrush>
                    </Image.OpacityMask>                    
                </Image>                                
                     
            <Slider 
                x:Name="mySlider"
                Background="Black" 
                Foreground="White" 
                Grid.Column="1"
                Grid.Row="1"
                Width="200"                 
                Margin="10" 
                Value="5.00"></Slider>
                
            </Grid>
            
            
            <TextBlock x:Name="mybox" Width="150" Height="50" Margin="5"></TextBlock>
            
        </StackPanel>
        
    </Grid>
</UserControl>


Page.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace LAOpacityMask2
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            mySlider.ValueChanged += new RoutedPropertyChangedEventHandler(mySlider_ValueChanged);
        }

        void mySlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
        {
            double d = e.NewValue - 5.00;

            double result = d / 10.00;
            Divider.SetValue(GradientStop.OffsetProperty, (0.5 + result));  
      
        }

 
    }
}