我有一个DataTable
列typeof(string)
。我正在向此表添加字符串,并且需要以粗体显示一些文本。我的字符串看起来像这样:字符串中的粗体文本
row["UserData"] = user.Name + "\n" + user.Description + "\n" + user.Number + "\t" + user.Date;
是否有可能在此代码中使user.Name
变为粗体?
我正在使用.NET 4.0和WPF。
我有一个DataTable
列typeof(string)
。我正在向此表添加字符串,并且需要以粗体显示一些文本。我的字符串看起来像这样:字符串中的粗体文本
row["UserData"] = user.Name + "\n" + user.Description + "\n" + user.Number + "\t" + user.Date;
是否有可能在此代码中使user.Name
变为粗体?
我正在使用.NET 4.0和WPF。
字符串只是文本,如果你想格式化文本,你有多种方式,但我不知道任何“本地”的东西。例如使用user.Name
左右的XML标记将格式编码为文本,然后某些控件可以使用它来显示它以调整渲染。
我99%确定您只能对DataTable的列进行样式设置,而不能对其中的数据进行样式设置。
DataTable只是一个数据存储 - 它不控制数据的显示。将ListView或其他WPF控件绑定到DataTable时,可以对控件的列进行样式设置,以粗体显示其中一列的文本。
这是显示在哪里?
这是一个纯文本串联的字符串,您试图使用粗体,是HTML还是要IDE中的user.Name部分代码显示为粗体?
如果是纯文本,那么您可以做的事情就不多了。在某些电子邮件客户端中显示字符串时,如果用星号(*)括起来,将显示为粗体。
对于HTML,你可以环绕user.Name与任何CSS或强/粗体标签:
row["UserData"] = "<strong>" + user.Name + </strong>" + "\n" + user.Description + "\n" + user.Number + "\t" + user.Date;
或
row["UserData"] = "<span style='font-weight: bold;'>" + user.Name + "</span>" + "\n" + user.Description + "\n" + user.Number + "\t" + user.Date;
我在我的WPF应用程序的用户界面中显示它。我的字符串在xaml.cs文件中定义。我只想让这个字符串的一部分变成粗体 - 这个是名字。 – Marta
加粗文字DataGridRow:WPF DataGrid Example
<Style x:Key="RowStyle" TargetType="{x:Type dg:DataGridRow}">
<Setter Property="ValidationErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Grid>
<Ellipse Width="12" Height="12"
Fill="Red" Stroke="Black"
StrokeThickness="0.5"/>
<TextBlock FontWeight="Bold" Padding="4,0,0,0"
Margin="0" VerticalAlignment="Top" Foreground="White" Text="!"
ToolTip="{Binding RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type dg:DataGridRow}},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
此link还显示如何加粗StackPanel TextBlock。
不,您需要使用RichTextBox进行复杂的格式设置。 – 0x4f3759df