There is a code for your control for Windows Form
Looks like a progress bar.

public partial class Slider : UserControl { public Slider() { InitializeComponent(); this.ForeColor = SystemColors.Highlight; } protected float percent = 0.0f; protected float maximumValue = 0.0f; public float Value { get { return percent; } set { if (value < 0) value = 0; //else if (value > 100) value = 0; percent = value; this.Invalidate(); } } public float Maximum { get { return maximumValue; } set { maximumValue = value; this.Invalidate(); } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Brush br = new SolidBrush(this.ForeColor); LinearGradientBrush lg = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height),Color.Cyan, Color.Blue, LinearGradientMode.ForwardDiagonal); int width = (int)((percent / maximumValue) * this.Width); e.Graphics.FillRectangle(br, 0, 0, width, this.Height); e.Graphics.FillRectangle(lg, 0, 0, width, this.Height); br.Dispose();lg.Dispose(); } } } 

It was made for a video player to display video playback.
So I wanted to redo it under WPF, but in WPF there is no OnPaint function.
I read that there is an OnRender method in WPF as an analogue, but I didn’t understand exactly how to do it.
Tell me, please, how to do it.

    1 answer 1

    You are doing wrong. Controls on WPF are written in a completely different way than on WinForms. OnRender do not need any OnRender , the layout manager should do everything for you.

    Get yourself a UserControl through the Visual Studio menu. You will create two files: Slider.xaml and Slider.xaml.cs .

    In Slider.xaml you need to place something like this:

     <UserControl x:Class="YourApplicationNamespace.Slider" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="0*" Name="LeftColumn"/> <ColumnDefinition Width="1*" Name="RightColumn"/> </Grid.ColumnDefinitions> <Border Grid.Column="0"> <Border.Background> <LinearGradientBrush EndPoint="0,0" StartPoint="1,1"> <GradientStop Color="Cyan" Offset="0"/> <GradientStop Color="Blue" Offset="1"/> </LinearGradientBrush> </Border.Background> </Border> </Grid> </UserControl> 

    And in Slider.xaml.cs you put two standard DependencyProperty Value and Maximum , and install UpdateValues on their update:

     public partial class Slider : UserControl { public Slider() { InitializeComponent(); } #region dp double Value public double Value { get { return (double)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } public static readonly DependencyProperty ValueProperty = DependencyProperty.Register( "Value", typeof(double), typeof(Slider), new PropertyMetadata(0.0, (o, args) => ((Slider)o).UpdateValues())); #endregion #region dp double Maximum public double Maximum { get { return (double)GetValue(MaximumProperty); } set { SetValue(MaximumProperty, value); } } public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register( "Maximum", typeof(double), typeof(Slider), new PropertyMetadata(100.0, (o, args) => ((Slider)o).UpdateValues())); #endregion void UpdateValues() { var ratio = Maximum <= 0 ? 0.0 : Value / Maximum; if (ratio < 0) ratio = 0; if (ratio > 1) ratio = 1; LeftColumn.Width = new GridLength(ratio, GridUnitType.Star); RightColumn.Width = new GridLength(1 - ratio, GridUnitType.Star); } } 

    Everything!

    Use this:

     <Window x:Class="YourApplicationNamespace.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:YourApplicationNamespace" Title="Test" Height="100" Width="350"> <Grid> <local:Slider Background="DarkGray" Value="80" Height="20"/> </Grid> </Window> 

    Result:

    looks dumb chesgrya