Changing StackPanel Background Using Coding4fun ColorSlider Control In UWP With XAML And C#

Before reading this article, please go through the following article.

  1. Introduction To Universal Windows Platform (UWP) App Development Using Windows 10 And Visual Studio 2015

ColorSlider is a UI component that displays a range of colors in a color sliding space. It is a kind of extended color picker which derives from ColorBaseControl and shows the selected color via its Color property.

Reading this article, you will learn how to use Coding4FunColorSlider in UWP app development, with XAML and Visual C#. Also, we will learn how to change the StackPanel background.

The following important tools are required for developing UWP.

  1. Windows 10 (Recommended)
  2. Visual Studio 2015 Community Edition (It is a free software available online).

Now, let's discuss step by step app development.

Step 1

Open Visual Studio 2015. Go to Start -> New Project-> Select Universal (under Visual C# -> Windows) -> Blank App -> Give suitable name to your app (UWPC4FColorSlider) -> OK.

Visual studio

After selecting the Target and Minimum platform version that your application will support, the Project creates App.xaml and MainPage.xaml.

Visual studio

Step 2

Open (double click) the file MainPage.xaml in Solution Explorer and add theCoding4Fun.Toolkit.Controls reference in the project.

For adding Reference, right click your project (UWPC4FColorSlider) and select "Manage NuGet Packages".

Visual studio

Choose "Browse" and search Coding4Fun.Toolkit.Controls . Select the package and install it.

Visual studio

Reference added to your project

Visual studio

Step 3

Add tab in the Toolbox. For adding Coding4Fun Toolkit controls, right click the Coding4Fun Toolkit (Newly added tab) and select "Choose items".

Visual studio

Select the path - C:\Users\<username>\.nuget\packages\Coding4Fun.Toolkit.Controls\2.1.8\lib\netcore451 

and select the file Coding4Fun.Toolkit.Controls.dll and filter the Coding4Fun.Toolkit controls.

Visual studio

Step 4

Add TextBlock control and change the name and text property for title.

Visual studio

Step 5

Add StackPanel control and change the name property.

Visual studio

Step 6

Now, add the ColorSlider Control and set the name and orientation property for Vertical Slider.

Visual studio

Step 7

Add one more ColorSlider Control and set the name and orientation property for Horizontal Slider.

Visual studio

Step 8

Add ColorChanged Event method for both the Colorslider.

Visual studio

Note Automatically, the following code will be generated in XAML code view, when we are done in the design view.

  1. <Page xmlns="" xmlns:x="" xmlns:local="using:UWPC4FColorSlider" xmlns:d="" xmlns:mc="" xmlns:Controls="using:Coding4Fun.Toolkit.Controls" x:Class="UWPC4FColorSlider.MainPage" mc:Ignorable="d">  
  2.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  3.         <TextBlock x:Name="tblTitle" HorizontalAlignment="Left" Margin="455,42,0,0" TextWrapping="Wrap" Text="Coding4Fun Color Slider Demo" VerticalAlignment="Top" Height="35" Width="369" FontWeight="Bold" FontSize="24" />  
  4.         <StackPanel x:Name="SPColSli" HorizontalAlignment="Left" Height="564" Margin="76,120,0,0" VerticalAlignment="Top" Width="1167">  
  5.             <Controls:ColorSlider x:Name="CSVer" Height="443" Margin="50,0,1044,0" Orientation="Vertical" ColorChanged="CSVer_ColorChanged" />  
  6.             <Controls:ColorSlider x:Name="CSHor" Height="61" Margin="201,0,61,0" Orientation="Horizontal" ColorChanged="CSHor_ColorChanged" />  
  7.         </StackPanel>  
  8.     </Grid>  
  9. </Page>  
Step 9

Add the code in ColorChanged event method in Mainpage.xaml.cs for changing StackPanel background.
  1. private void CSVer_ColorChanged(object sender, Windows.UI.Color color) {  
  2.     SPColSli.Background = new SolidColorBrush(color);  
  3. }  
  4. private void CSHor_ColorChanged(object sender, Windows.UI.Color color) {  
  5.     SPColSli.Background = new SolidColorBrush(color);  
  6. }  
Visual studio

Step 10

Deploy your app in Local Machine. The output of the UWPC4FColorSlider app is shown below.

Visual studio

Sliding the color from Horizontal ColorSlider

Visual studio

Sliding the color from Vertical ColorSlider.

Visual studio


Now, you have successfully tested Code4Fun ToolKit – Colorslider in Visual C# - UWP environment and changed the StackPanel background color.