数据绑定 TextBlock 在 Silverlight/WP7 中运行

我在 Windows Phone 7 上使用 Silverlight.

I'm using Silverlight on Windows Phone 7.

我想以粗体显示 TextBlock 中某些文本的第一部分,其余部分以普通字体显示.完整的文本必须换行.我希望粗体部分包含来自我的 ViewModel 中一个属性的文本,而纯文本包含来自不同属性的文本.

I want to display the first part of some text in a TextBlock in bold, and the rest in normal font. The complete text must wrap. I want the bolded part to contain text from one property in my ViewModel, and the plain text to contain text from a different property.

TextBlock 在与 LongListSelector 关联的 DataTemplate 中定义.

The TextBlock is defined in a DataTemplate associated with a LongListSelector.

我最初的尝试是:

<TextBlock TextWrapping="Wrap">
  <TextBlock.Inlines>
    <Run Text="{Binding Property1}" FontWeight="Bold"/>
    <Run Text="{Binding Property2}"/>
  </TextBlock.Inlines>
</TextBlock>

这在运行时失败,并显示非常无用的AG_E_RUNTIME_MANAGED_UNKNOWN_ERROR".这是一个已知问题,因为 Run 元素不是 FrameworkElement 并且无法绑定.

This fails at runtime with the spectacularly unhelpful "AG_E_RUNTIME_MANAGED_UNKNOWN_ERROR". This is a known issue because the Run element is not a FrameworkElement and cannot be bound.

我的下一个尝试是放置占位符,然后在代码中更新它们:

My next attempt was to put placeholders in place, and then update them in code:

<TextBlock Loaded="TextBlockLoaded" TextWrapping="Wrap">
    <TextBlock.Inlines>
        <Run FontWeight="Bold">Placeholder1</Run>
        <Run>Placeholder2</Run>
    </TextBlock.Inlines>
</TextBlock>

在代码隐藏中(是的,我很绝望!):

In the code-behind (yes I am desparate!):

private void TextBlockLoaded(object sender, RoutedEventArgs e)
{
    var textBlock = (TextBlock)sender;
    var viewModel = (ViewModel)textBlock.DataContext;
    var prop1Run = (Run)textBlock.Inlines[0];
    var prop2Run = (Run)textBlock.Inlines[1];
    prop1Run.Text = viewModel.Property1;
    prop2Run.Text = viewModel.Property2;
}

这似乎有效,但因为我使用的是 LongListSelector,虽然项目被回收,但 Loaded 代码隐藏事件处理程序不会重新初始化运行,所以很快就会显示错误的文本...

This seemed to work, but because I am using the LongListSelector, although items get recycled, the Loaded codebehind event handler doesn't re-initialize the Runs, so very quickly the wrong text is displayed...

我已经研究过使用 LongListSelector 的 Linked 事件(我已经用它来释放我在列表中显示的图像),但我看不到如何使用它来重新初始化 Runs 的文本属性.

I've looked at using the LongListSelector's Linked event (which I already use to free up images that I display in the list), but I can't see how I can use that to re-initialize the Runs' text properties.

感谢任何帮助!



 1 条回答