I am trying to get a label to show specific text while also being bound to a variable in the VB.Net code. I can make a binding but I cant get it to add the static text.
What I have so far:
<Label x:Name="TestLabel" Content="{Binding Path=Row, StringFormat='Row #{0}'}"
HorizontalAlignment="Left"
Height="35"
Margin="203,21,0,0"
VerticalAlignment="Top"
Width="83"
FontSize="18">
with
Public Class Row
Implements INotifyPropertyChanged
Private _Row As Byte
Public Property Row() As Byte
Get
Return _Row
End Get
Set(ByVal value As Byte)
_Row = value
OnPropertyChanged(New PropertyChangedEventArgs("Row"))
End Set
End Property
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
If Not PropertyChangedEvent Is Nothing Then
RaiseEvent PropertyChanged(Me, e)
End If
End Sub
End Class
and
Private Rows As New Row
Public Sub New()
InitializeComponent()
TestLabel.DataContext = Rows
Rows.Row = MyTextBox.Text.HandledStringtoSByte
End Sub
The extension code (since I have a custom extension):
''' <summary>
''' Handles conversion of string variable to Tiny Integer
''' </summary>
''' <param name="s"></param>
''' <param name="I">Returned if conversion fails.</param>
''' <returns>Signed 8bit Integer</returns>
''' <remarks></remarks>
<Runtime.CompilerServices.Extension()> _
Public Function HandledStringtoSByte(ByRef S As String, Optional I As SByte = 0) As SByte
Try
If S = String.Empty Then
Return I
Else
Return SByte.Parse(S)
End If
Catch
Dim result As String = String.Empty
Dim ReturnByte As SByte
Dim Parsed As Byte
For Each Character In S.ToCharArray
If Character = "-" Then
If S.Substring(0, 1).ToString <> "-" Then
Exit For
End If
End If
If Character = "." Then
Exit For
End If
If Byte.TryParse(Character, Parsed) Then
result = result + Parsed.ToString
End If
Next
If result <> String.Empty Then
If SByte.TryParse(result, ReturnByte) Then
Return SByte.Parse(ReturnByte)
Else
If Short.Parse(result) > Short.Parse(SByte.MaxValue.ToString) Then
Return SByte.MaxValue
ElseIf Short.Parse(result) < Short.Parse(SByte.MinValue.ToString) Then
Return SByte.MinValue
Else
Return SByte.Parse(ReturnByte)
End If
End If
Else
Return I
End If
End Try
End Function
Now I thought that using the stringformat in binding would add the static text and place the bound variable into the {0} spot but all is gives me is the bound variable in the label.
What am i doing wrong?
Binding target is Content property which is Object type, that is why you cannot use StringFormat with binding.
Instead use ContentStringFormat property
<Label Content="{Binding Path=Row}"
ContentStringFormat="Row #{0}" />
Another approach: create readonly property in the ViewModel which will represent value in wanted format
Private _Row As Byte
Public Property Row() As Byte
Get
Return _Row
End Get
Set(ByVal value As Byte)
_Row = value
OnPropertyChanged(New PropertyChangedEventArgs("Row"))
OnPropertyChanged(New PropertyChangedEventArgs("RowText"))
End Set
End Property
Public ReadOnly Property RowText As String
Get
Return String.Format("Row #{0}", Me.Row)
End Get
End Property
Then bind this property to the View
<Label Content="{Binding Path=RowText}"/>
The problem is that Binding.StringFormat is "a string that specifies how to format the binding if it displays the bound value as a string". In practice it seems to work only if the target property is of type string - as you pointed out it's working for TextBlock.Text (which is of type string) and not for Label.Content (which is of type object). There are several ways to approach this problem, one of them would be to nest a TextBlock in the Content property:
<Label>
<TextBlock Text="{Binding Path=Row, StringFormat='Row #{0}'}" />
</Label>
This doesn't really introduce any additional complexity to the visual tree since strings are by default presented by TextBlocks.
Otherwise you could create your own converter, or you could go with Fabio's solution and utilize Label.ContentStringFormat property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With