Reputation: 3
I develop silverlight app for windows phone 8. I bind integer value in LongListSelector for example 123. And I want to make it looks like 000123
<phone:LongListSelector ItemsSource="{Binding AllValues}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock x:Name="ValueBlock"
Text="{Binding ValueValue}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Style="{StaticResource PhoneTextLargeStyle}"/>
...
I know StringFormat can do this, but the problem is number of leading zeros is always different and I want to bind it from ViewModel.
...
Text="{Binding ValueValue}, StringFormat=\{0:D6\}"
...
How can I bind a number after D in StringFormat?
Upvotes: 0
Views: 97
Reputation: 39007
You can't, but you're taking the wrong approach anyway. If you want your viewmodel to decide how your value is displayed, then have your viewmodel handle the whole process of formatting the value. For instance:
public string Value
{
get
{
return string.Format("{0:D" + this.NumberOfZero + "}", this.value);
}
}
Then bind your view directly to the formatted property.
If your property is a numeric value, you can also directly write:
public string Value
{
get
{
return this.value.ToString("D" + this.NumberOfZero);
}
}
Upvotes: 1
Reputation: 102743
You can't bind the StringFormat
part of a binding expression. What you can do instead is add another property to the view model like "ValueDisplay".
public string ValueDisplay
{
get { return string.Format("{0:" + ValueFormat + "}", ValueValue); }
}
public string ValueFormat
{
get { return _valueFormat; }
set
{
_valueFormat = value;
RaisePropertyChanged("ValueDisplay");
}
}
private string _valueFormat;
Upvotes: 1