Click here to Skip to main content
15,887,214 members
Articles / Desktop Programming / WPF

How to Build a Custom Formatted Label with WPF

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
1 Nov 2007CPOL1 min read 35.1K   331   19  
This article explains how to provide a custom gauge formatter for WPF TextBlocks, Label or any other controls
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;


namespace GaugeFormatter
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class Window1 : System.Windows.Window
    {

        public Window1()
        {
            InitializeComponent();
        }

        void ReDo(object s, RoutedEventArgs e)
        {
            ObjectDataProvider odp = Resources["data"] as ObjectDataProvider;
            ((DataSource)odp.Data).Init();
        }

    }
    class DataConverter:IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int val = (int)value;
            int max = int.Parse(parameter.ToString());
            return new Point(val == 0 ? 0 : ((double)val / (double)max), 0.5);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
    class DataSource : ObservableCollection<int>
    {
        Random rnd = new Random();
        public DataSource()
        {
            Init();
        }

        public DataSource(int max)
        {
            Init(max);
        }

        public void Init()
        {
            Init(1000);
        }

        public void Init(int max)
        {
            this.Clear();
            for (int i = 0; i < 10; i++)
            {
                this.Add(rnd.Next(max));
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Better Place
Israel Israel
Hello! My name is Tamir Khason, and I am software architect, project manager, system analyst and [of course] programmer. In addition to writing big amount of documentation, I also write code, a lot of code. I used to work as a freelance architect, project manager, trainer, and consultant here, in Israel, but recently join the company with extremely persuasive idea - to make a world better place. I have very pretty wife and 3 charming kids, but unfortunately almost no time for them.

To be updated within articles, I publishing, visit my blog or subscribe RSS feed. Also you can follow me on Twitter to be up to date about my everyday life.

Comments and Discussions