Customizing string format in a Caliburn.Micro convention bound TextBox
Asked Answered
W

1

7

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?

Wenn answered 16/8, 2012 at 12:1 Comment(3)
The short answers is no. Either you use the binding generated by Caliburn or you have to write out the whole binding expression as in your sample.Koblenz
Distance doesn't have to be a number, it's a model of what you want to be in the view. So don't make the property a Double, make it be the string you want in the view.Rid
Failing that just bind everything manually - the only thing that gets bound by name convention is the default bindable prop or the action. One binding isn't too horrid to look at is it?Ephram
C
4

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.

Caftan answered 23/1, 2014 at 4:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.