Does StringFormat feature of WPF Xaml work on Label.Content? - wpf

I have bind my Amount Label's Content Property to a decimal property via DataContext. I am trying to apply stringformat but see no effect. Does StringFormat feature work on Label controls ?? Please tell me on which controls does this feature work. BTW following is the code for the Label Control for whom i want to apply the currency formatting
<Label Grid.Column="2" Content="{Binding Path=Amount, StringFormat={}{0:C}}" Height="23" HorizontalAlignment="Left" Margin="100,10,0,0" Name="tb" VerticalAlignment="Bottom" Width="120" />

StringFormat works on properties of type string (when the object you are binding to is being converted to a string the string format is applied). The Content property is of type Object.
You can place a TextBlock inside your label to achieve the desired effect:
<Label Grid.Column="2" Height="23" HorizontalAlignment="Left" Margin="100,10,0,0" Name="tb" VerticalAlignment="Bottom" Width="120">
<TextBlock Text="{Binding Path=Amount, StringFormat={}{0:C}}"/>
</Label>

Try ContentStringFormat
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/866f7934-8b10-4872-b306-122674fad5fa/
<Label Content=ā€{Binding Amount}ā€ ContentStringFormat=ā€Cā€ />

Related

WPF text StringFormat as DynamicResource

So i have this TextBlock:
<TextBlock
Text="{Binding Path=Value, ElementName=progressBarColumn, StringFormat={}{0:N2}%}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="{DynamicResource ProgressBarForegroundColor}"
FontFamily="{DynamicResource ProgressBarFontFamily}"
FontSize="{DynamicResource ProgressBarFontSize}"/>
And i want to be able to control this String Format from N2 to N1 etc. so i created this :
<system:String x:Key="ProgressBarStringFormat">N2</system:String>
Usage:
Text="{Binding Path=Value, ElementName=progressBarColumn, StringFormat={}{0:ProgressBarStringFormat}%}"
And in my Progress-Bar instead of seen the Value i only see ProgressBarStringFormat text.
You can't have anything but a literal for a property of a Binding. But if you're willing to use a ContentControl or a Label instead of a TextBlock, you can stick a DynamicResource or a Binding on the ContentStringFormat property thereof:
<Label
Margin="0"
Content="{Binding Value, ElementName=progressBarColumn}"
ContentStringFormat="{DynamicResource ProgressBarStringFormat}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="{DynamicResource ProgressBarForegroundColor}"
TextElement.FontFamily="{DynamicResource ProgressBarFontFamily}"
TextElement.FontSize="{DynamicResource ProgressBarFontSize}"
/>
I'm setting Margin to zero because Label will have a default margin set in its implicit style, unlike TextBlock.

Bind ObservableCollection count to wpf label and format the label

I have a label where I want to show the number of selected items in a list ( list items are selected from a grid ). That all works well and the label displays the number. What I want is to make the label display "5 items selected". Right now, I just get the number 5. Here is the xaml:
<Label Height="23" HorizontalAlignment="Left" Margin="7,2,0,0" Name="lblSelectionSummary" VerticalAlignment="Top" Width="557" FontFamily="Arial" >
<Label.Content>
<Binding Path="SelectedRows.Count" />
</Label.Content>
</Label>
I'm close on this guy.
you just need to specify the StringFormat in your binding
this should do
<Binding Path="SelectedRows.Count" StringFormat="{}{0} items selected"/>
above may not work for Label as it follows Content model , so you may need to use TextBlock instead
sample
<TextBlock Height="23" HorizontalAlignment="Left" Margin="7,2,0,0" Name="lblSelectionSummary" VerticalAlignment="Top" Width="557" FontFamily="Arial" >
<TextBlock.Text>
<Binding Path="SelectedRows.Count"
StringFormat="{}{0} items selected"/>
</TextBlock.Text>
</TextBlock>
or
<TextBlock Height="23"
HorizontalAlignment="Left"
Margin="7,2,0,0"
Name="lblSelectionSummary"
VerticalAlignment="Top"
Width="557"
FontFamily="Arial"
Text="{Binding SelectedRows.Count, StringFormat={}{0} items selected}" />
Use string format with a Label
StringFormat in binding works for string type properties, and since type of Content property of Label is object so StringFormat does not work
thanks to blindmeis for the hint
since a Label follows Content model it uses ContentStringFormat to format the value, below is an example to use the same
<Label Content="{Binding SelectedRows.Count}"
ContentStringFormat="{}{0} items selected" />

ReadOnly DatePicker in silverlight

I am making an application in Silverlight 3.0. In that application i am using DatePicker control as
<TextBlock FontSize="13" Height="23" Name="txtFromDate" Text="FromDate" Width="40" Canvas.Left="444" Canvas.Top="6" />
<controls:DatePicker Name="fromdatePicker" Height="23" Width="110" Canvas.Left="550" Canvas.Top="4" />
Here i don't want to enter date manually(Means the textblock txtFromDate should be read only.)And the value in text block will be whatever date is selected from calender. I am not getting how to do it? Please help me.Thanks in advance.
Assuming you're using a nice MVVM approach:
Bind the SeletectedDate of your date picker to a DateTime property of your ViewModel (two-way binding):
<sdk:DatePicker SelectedDate="{Binding MyDate, Mode=TwoWay}"/>
Bind the Text property of your TextBlock to the same property:
<TextBlock Text="{Binding MyDate}" />

Validation is not working in Textbox

I have a TExtBox and have created a ValidationRule class from here and this is my xaml :
<TextBox Name="ctsTxt" Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}"
local:SimpleValidator.ValidationType="{x:Type system:Double}"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource txtBoxStyle}"
Grid.Column="1" Grid.Row="2" Margin="2"
/>
As per this and the validator class, the textbox sh accept only double input, but it accepts everything.
What's wrong in xamls that my validation is not happening at all.
Any help is appreciated.
Did you try setting your Text Binding like this?
Text="{Binding Text, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"

Why is binding result different on a Label and a TextBox?

I was using the following XAML:
<Label Grid.Row="0" Grid.Column="0" Content="Datum"/>
<Label Grid.Row="0" Grid.Column="1" Content="{Binding TimeStamp, StringFormat={}{0:yyyy-MM-dd HH:mm:ss.fff}}"/>
<Label Grid.Row="0" Grid.Column="2" Content="Level"/>
<Label Grid.Row="0" Grid.Column="3" Content="{Binding Level}"/>
but the TimeStamp was being formatted like this:
2.24.2012 7:38
I started up Snoop (great tool!) and noticed that the Label is actually composed of a TextBox and that this TextBox contained the TimeStamp formatted as I defined it. I then replaced the Label with a TextBox and I get the TimeStamp correctly formatted.
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding TimeStamp, StringFormat={}{0:yyyy-MM-dd HH:mm:ss.fff}}"/>
2012-02-24 07:38:23.123
I have defined no Resource, Trigger or Style blocks to override Label behaviour so I'm wondering why this is happening.
Any ideas?
The Binding.StringFormat property doesn't work on Labels, you need to use the ContentStringFormat property on the Label
<Label Grid.Row="0" Grid.Column="1" Content="{Binding TimeStamp}">
<Label.ContentStringFormat>0:yyyy-MM-dd HH:mm:ss.fff</Label.ContentStringFormat>
</Label>
also see Binding only part of a label

Resources