我需要使用WPF数据绑定(值更改)显示以下内容。标题必须是粗体,信息行是正常的文字。如果给定标题的信息不存在,我想折叠该部分,包括标题。我更喜欢所有的数据(标题和信息项)在一个格式化的字符串中,可以在我想要的地方换行。WPF使用数据绑定显示格式化多行文本
头1:
我的信息1
我的信息2
头2:
我的信息3
我的信息4
我需要使用WPF数据绑定(值更改)显示以下内容。标题必须是粗体,信息行是正常的文字。如果给定标题的信息不存在,我想折叠该部分,包括标题。我更喜欢所有的数据(标题和信息项)在一个格式化的字符串中,可以在我想要的地方换行。WPF使用数据绑定显示格式化多行文本
头1:
我的信息1
我的信息2
头2:
我的信息3
我的信息4
如果你想在一个风格做粗体,我认为你最好的选择是打破你的字符串,并在Expander
内使用TextBlocks
内StackPanel
。
另外,你可以用你的整个字符串做RichTextBox
,但我认为你的字符串必须包含<bold></bold>
标签。
另一种尝试方法。使用TextBlock.Inlines。然后将您的模型绑定到TextBlock,并在自定义value converter中或通过自定义附加属性解析您的模型以填充TextBlock的内联。
这里是一个需要文本字符串,使每第二个字加粗附加属性的例子:
public class RunExtender : DependencyObject
{
public static string GetText(DependencyObject obj)
{
return (string)obj.GetValue(TextProperty);
}
public static void SetText(DependencyObject obj, string value)
{
obj.SetValue(TextProperty, value);
}
public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached("Text", typeof(string), typeof(RunExtender), new PropertyMetadata(string.Empty, OnBindingTextChanged));
private static void OnBindingTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var bindingText = e.NewValue as string;
var text = d as TextBlock;
if (text != null)
{
text.Inlines.Clear();
var words = bindingText.Split(' ');
for (int i = 0; i < words.Length; i++)
{
var word = words[i];
var inline = new Run() {Text = word + ' '};
if (i%2 == 0)
{
inline.FontWeight = FontWeights.Bold;
}
text.Inlines.Add(inline);
}
}
}
}
这不是一个生产质量的代码,它是从Silverlight的演示拍摄,但你的想法。
希望这会有所帮助。
干杯,Anvaka。