gpt4 book ai didi

wpf - 带有静态文本和绑定(bind)的标签

转载 作者:行者123 更新时间:2023-12-03 23:23:16 26 4
gpt4 key购买 nike

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

到目前为止我所拥有的:

<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">



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



Private Rows As New Row

Public Sub New()
InitializeComponent()
TestLabel.DataContext = Rows
Rows.Row = MyTextBox.Text.HandledStringtoSByte
End Sub

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

''' <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

现在我认为在绑定(bind)中使用 stringformat 会添加静态文本并将绑定(bind)变量放入 {0} 点,但给我的只是标签中的绑定(bind)变量。

我究竟做错了什么?

最佳答案

绑定(bind)目标是Content属性是 Object类型,这就是为什么你不能使用 StringFormat具有约束力。

而是使用 ContentStringFormat属性(property)

<Label Content="{Binding Path=Row}"  
ContentStringFormat="Row #{0}" />

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

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

然后将此属性绑定(bind)到 View
<Label Content="{Binding Path=RowText}"/>

关于wpf - 带有静态文本和绑定(bind)的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34621376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com