Tono Nam
Tono Nam

Reputation: 36080

Format binding value with c#

In my application I am binding several text boxes to properties. So in c# I have:

public class MyUserControl : UserControl, INotifyPropertyChanged
{
    decimal _Price;
    public decimal Price
    {
       get { return _Price; }
       set
       {
          _Price = value;
          OnPropertyChanged("Price");
       }
    }

    // implement interface
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    // etc

and on xaml I have:

   <TextBox Name="txtPrice" DataContext="{Binding}" Text="{Binding Price, UpdateSourceTrigger=PropertyChanged, StringFormat=c}"></TextBox>

then If in my code behind I set the Price = 12.22 for example it will display $12.22 in the textbox.


Now because I am using this behavior very often I want to create a class that will create the property binded to the textbox for me. So my class looks like:

public class ControlBind<T> : INotifyPropertyChanged
{
    protected T _Value;
    public T Value
    {
        get { return _Value; }
        set
        {
            _Value = value;
            OnPropertyChanged("Value");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
                protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }



    public ControlBind(Control control, System.Windows.DependencyProperty controlPropertyToBind)
    {
        Binding b = new Binding("Value")
        {
            Source = this
        };

        b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        control.SetBinding(controlPropertyToBind, b);
    }
}

then I will be able to use that class and create the same behavior by doing:

// txtPrice is a textbox defined in xaml
ControlBind<decimal> Price = new ControlBind<decimal>(txtPrice, TextBox.TextProperty);

Price.Value = 45; // update textbox value to "45"

but now I don't know how to do the string format. When I use the class, the textbox prints 45 and I want it to print $45

So in other words how can I achieve xaml binding {Binding Price, StringFormat=c} in code behind

Upvotes: 1

Views: 2095

Answers (3)

Henk Holterman
Henk Holterman

Reputation: 273691

Not tested, but I think:

 Binding b = new Binding("Value")
 {
      Source = this,
      StringFormat = "c"
 };

Upvotes: 1

Dave S
Dave S

Reputation: 775

To do it purely in code you can just do Price.ToString("C") otherwise @Tigran's solution using converters is the way you want to go.

Upvotes: 0

Tigran
Tigran

Reputation: 62265

You should be able to do it via Converters.

Can try to code something like this from code behind:

public ControlBind(Control control, System.Windows.DependencyProperty controlPropertyToBind)
{
   Binding b = new Binding("Value")
   {
     Source = this,
     Converter = new MyCurrencyConverter() //Converter
   };

   b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
   control.SetBinding(controlPropertyToBind, b);
}

Where MyCurrencyConverter converts your 45 to $45.

Hope this helps.

Upvotes: 1

Related Questions