我创建了一个新的 TextBlock 类,它具有 ItemsSource 属性并将 ItemsSource 转换为“Run”对象:
public class MultiTypeDynamicTextBlock : TextBlock
{
    public interface ISection
    {
        Inline GetDisplayElement();
    }
    public class TextOption : ISection
    {
        private Run mText;
        public TextOption(string aText)
        {
            mText = new Run();
            mText.Text = aText.Replace("\\n", "\n");
        }
        public Inline GetDisplayElement()
        {
            return mText;
        }
    }
    public class LineBreakOption : ISection
    {
        public Inline GetDisplayElement()
        {
            return new LineBreak();
        }
        public ISection Clone()
        {
            return new LineBreakOption();
        }
    }
    public class ImageOption : ISection
    {
        private InlineUIContainer mContainer;
        public ImageOption(string aDisplay)
        {
            Image lImage;
            lImage = new Image();
            lImage.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + aDisplay));
            lImage.Height = 15;
            lImage.Width = 15;
            mContainer = new InlineUIContainer(lImage);
        }
        public Inline GetDisplayElement()
        {
            return mContainer;
        }
    }
    public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register("ItemsSource", typeof(ObservableCollection<ISection>), typeof(MultiTypeDynamicTextBlock),
        new UIPropertyMetadata(new ObservableCollection<ISection>(),
        new PropertyChangedCallback(SetContent)));
    public ObservableCollection<ISection> ItemsSource
    {
        get
        {
            return GetValue(ItemsSourceProperty) as ObservableCollection<ISection>;
        }
        set
        {
            if (ItemsSource != null)
                ItemsSource.CollectionChanged -= CollectionChanged;
            SetValue(ItemsSourceProperty, value);
            SetContent();
            ItemsSource.CollectionChanged += CollectionChanged;
        }
    }
    private void CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        SetContent();
    }
    private static void SetContent(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        DependencyObject lParent = d;
        MultiTypeDynamicTextBlock lPanel = lParent as MultiTypeDynamicTextBlock;
        if (lPanel != null)
        {
            lPanel.ItemsSource = e.NewValue as ObservableCollection<ISection>;
        }
    }
    private void SetContent()
    {
        if (ItemsSource != null)
        {
            Inlines.Clear();
            foreach (ISection lCurr in ItemsSource)
            {
                Inlines.Add(lCurr.GetDisplayElement());
            }
        }
    }
如果我将 ItemsSource 直接绑定到 DataContext,它就可以工作。但是,如果我将它绑定到在运行时更改的对象(例如 ListBox 上的 SelectedItem),它不会在选择新项目时更新文本。
<StackPanel>
    <ListBox x:Name="TheList" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Title}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <StackPanel DataContext="{Binding ElementName=TheList, Path=SelectedItem}">
        <TextBlock Text="{Binding Title}" FontSize="20"/>
        <local:MultiTypeDynamicTextBlock ItemsSource="{Binding Items}"/>
    </StackPanel>
</StackPanel>
有什么理由吗?