Click here to Skip to main content
15,885,757 members
Articles / Desktop Programming / WPF

Bindable Converter Parameter

Rate me:
Please Sign up or sign in to vote.
4.81/5 (8 votes)
2 Jul 2013CPOL7 min read 120K   4.5K   28  
A simple technique to achieve Bindable-ConverterParameter in WPF's XAML.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Timers;

namespace BindableConverterParameterTake2
{
    class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            _FirstName = "ABC";
            _LastName = "DEF";

            Timer t = new Timer(2000);
            bool state = false;
            t.Elapsed += new ElapsedEventHandler((s, e) =>
            {
                if (state)
                {
                    ConcatSign = "-";
                }
                else
                {
                    ConcatSign = " ";
                }
                state = !state;
            });
            t.Start();
        }
        private string _FirstName;
        public string FirstName
        {
            get { return _FirstName; }
            set { SetProperty(ref _FirstName, value, "FirstName"); }
        }

        private string _LastName;
        public string LastName
        {
            get { return _LastName; }
            set { SetProperty(ref _LastName, value, "LastName"); }
        }

        private string _ConcatSign;
        public string ConcatSign
        {
            get { return _ConcatSign; }
            set { SetProperty(ref _ConcatSign, value, "ConcatSign"); }
        }




        private void SetProperty<T>(ref T field, T value, string propertyName)
        {
            if (!EqualityComparer<T>.Default.Equals(field, value))
            {
                field = value;
                var handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;


    }
}

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
Software Developer (Senior) self employed
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions