带有静态文本和绑定的标签

ARi*_*101 2 vb.net data-binding wpf xaml

我试图获取一个标签来显示特定文本,同时还绑定到VB.Net代码中的变量。我可以进行绑定,但是无法添加静态文本。

到目前为止,我有:

<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">
Run Code Online (Sandbox Code Playgroud)

<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">
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

扩展代码(因为我有一个自定义扩展名):

Private Rows As New Row

Public Sub New()
    InitializeComponent()
    TestLabel.DataContext = Rows
    Rows.Row = MyTextBox.Text.HandledStringtoSByte
End Sub
Run Code Online (Sandbox Code Playgroud)

现在,我认为在绑定中使用stringformat会添加静态文本,并将绑定变量放置在{0}位置,但是所有给我的就是标签中的绑定变量。

我究竟做错了什么?

Fab*_*bio 5

绑定目标是类型的Content属性Object,这就是为什么不能StringFormat与绑定一起使用的原因。

而是使用ContentStringFormat属性

<Label Content="{Binding Path=Row}"  
       ContentStringFormat="Row #{0}" />
Run Code Online (Sandbox Code Playgroud)

另一种方法:在ViewModel中创建只读属性,该属性将以所需格式表示值

<Label Content="{Binding Path=Row}"  
       ContentStringFormat="Row #{0}" />
Run Code Online (Sandbox Code Playgroud)

然后将此属性绑定到视图

<Label Content="{Binding Path=RowText}"/>
Run Code Online (Sandbox Code Playgroud)