FormattedText.FormttedText is obsolete. Use the PixelsPerDip override

nikhil picture nikhil · Aug 19, 2017 · Viewed 11.8k times · Source

I am trying to populate labels to a horizontal slider and I was successfully able to do it using a class that derives from TickBar by passing the Text to FormattedText constructor. But now when I take the same code and paste it in Visual Studio that uses .NET Framework version 4.6.2, it says:

FormattedText.FormttedText is obsolete. Use the PixelsPerDip override.

I referred to In .NET Framework 4.6.2 the FormattedText() is Obsoleted, how can i fixed it

But how can I use that in this current case. Please help.

FormattedText formattedText = new FormattedText(text, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Verdana"), 16, Brushes.Black);
dc.DrawText(formattedText, new Point((tickFrequencySize * i), 30)); //dc is Drawing Context.

Here is the complete class:

public class CustomTickBar : TickBar
    {
        public static string FontTextList { get; set; }
        protected override void OnRender(DrawingContext dc)
        {
            //string str = "Small, Medium, Large, Extra\n Large";
            if (!string.IsNullOrEmpty(FontTextList))
            {
                string[] ar = FontTextList.Split(',');

                Size size = new Size(base.ActualWidth, base.ActualHeight);
                int tickCount = (int)((this.Maximum - this.Minimum) / this.TickFrequency) + 1;
                //int tickCount = 4; 
                if ((this.Maximum - this.Minimum) % this.TickFrequency == 0)
                    tickCount -= 1;
                Double tickFrequencySize;
                // Calculate tick's setting
                tickFrequencySize = (size.Width * this.TickFrequency / (this.Maximum - this.Minimum));
                string text = "";
                FormattedText formattedText = null;
                double num = this.Maximum - this.Minimum;
                int i = 0;
                // Draw each tick text
                for (i = 0; i <= tickCount; i++)
                {

                    //text = Convert.ToString(Convert.ToInt32(this.Minimum + this.TickFrequency * i), 10);
                    text = ar[i];
                    formattedText = new FormattedText(text, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Verdana"), 16, Brushes.Black);
                    dc.DrawText(formattedText, new Point((tickFrequencySize * i), 30));


                }
            }
        }

Answer

Olexiy Sadovnikov picture Olexiy Sadovnikov · Aug 19, 2017

Try this:

FormattedText formattedText = new FormattedText(text, CultureInfo.GetCultureInfo("en-us"),
    FlowDirection.LeftToRight, new Typeface("Verdana"), 16, Brushes.Black,
    VisualTreeHelper.GetDpi(this).PixelsPerDip);