Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing string format in a Caliburn.Micro convention bound TextBox

I have a WPF TextBox that is bound to a view-model number property Distance through Caliburn.Micro naming conventions. I want to be able to customize the TextBox string format while preserving the convention binding that Caliburn.Micro has set up. How should I do that?

From my View Model:

public double Distance
{
    get { return _distance; }
    set
    {
        _distance = value;
        NotifyOfPropertyChange(() => Distance);
    }
}

From my View:

<TextBox x:Name="Distance"/>

When the Distance is non-zero, I want to display the number with a fixed set of decimals, and when the Distance is zero I want the text box to be empty.

Using explicit binding I can bind the TextBox.Text property to Distance, and then I can set the StringFormat simultaneously:

<TextBox x:Name="Distance" Text="{Binding Distance, StringFormat=0.000;;#}"/>

However, the explicit Text binding would then short-circuit the Caliburn.Micro naming convention binding. Is it possible to customize the string format without simultaneously having to set the binding path of the TextBox.Text property, so that I can solely rely on Caliburn.Micro to handle the Distance-to-TextBox binding?

like image 830
Anders Gustafsson Avatar asked Aug 16 '12 12:08

Anders Gustafsson


1 Answers

There is no possible way of what you want to do. The simplest way is that you provide yourself. The second way is to expose the string property in the ViewModel and preformat it in the getter.

like image 175
EngineerSpock Avatar answered Sep 17 '22 21:09

EngineerSpock