在文本块中显示带有字符串的文本xaml
问题描述:
我有一个文本块,我想用一个定义的字符串显示文本。怎么做?在文本块中显示带有字符串的文本xaml
的文本块:
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Padding="6" VerticalAlignment="Center" Margin="45,0,0,0" Height="30" Width="386" Text="My Program ver. Version"/>
我的字符串:
Public Version As String = "1.0a"
答
在XAML文件:
首先,你应该为您的TextBlock我已经给了tbWithNoName
例如。
<TextBlock x:Name="tbWithNoName" HorizontalAlignment="Left" TextWrapping="Wrap" Padding="6" VerticalAlignment="Center" Margin="45,0,0,0" Height="30" Width="386" Text="My Program ver. Version"/>
然后在Window对象上添加Loaded调用。
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
Loaded="Window_Loaded">
将Window_Loaded函数插入到您的vb文件。
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
tbWithNoName.Text = tbWithNoName.Text + " " + Version
End Sub
这将改变TextBlock中的文本时,窗口中加载
答
您可以使用StringFormat。
<TextBlock Text="{Binding Path=Version, StringFormat=My Program ver. {0}}" />
在你后面的代码 - 必须改变Version
财产(这个属性应该是ReadOnly
,因为它不是在运行时更改),并在构造函数分配DataContext
:
Class MainWindow
Public Sub New()
InitializeComponent()
Me.DataContext = Me
End Sub
ReadOnly Property Version As String
Get
Return "1.0a"
End Get
End Property
End Class
答
如果你想你TextBlock在每次有新版本时更新版本号,
你可以在C#中这样做。你可以很容易地找到如何在VB中编写这个。
这会在您每次发布程序的新版本时更新您的TextBlock。
在XAML绑定一个TextBlock文本“版本”:
<TextBlock Text="{Binding Version, Mode=OneWay}" />`
然后在后台代码或在您的视图模型,你可以在XAML TextBlock的使用属性,用于绑定,你有:
public string Version
{
get
{
return String.Format("VERSION: {0}",DeploymentInfo.Version.ToString());
}
}
然后,你需要参考您的项目添加到“System.Deployment”。
这只有在您完成项目的“发布”时才有效。当您启动调试器时,您可能只会看到版本号:0.0.0.0