1

我创建了一个新的 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>

有什么理由吗?

4

2 回答 2

0

谢谢您的帮助。我设法通过更新 MultiTypeDynamicTextBlock 来解决这个问题,如下所示:

public class MultiTypeDynamicTextBlock : TextBlock
{
    public interface ISection
    {
        Inline GetDisplayElement();

        ISection Clone();
    }

    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 ISection Clone()
        {
            return new TextOption(mText.Text);
        }
    }

    public class LineBreakOption : ISection
    {
        public Inline GetDisplayElement()
        {
            return new LineBreak();
        }

        public ISection Clone()
        {
            return new LineBreakOption();
        }
    }

    public class SectionList
    {
        private ObservableCollection<ISection> mList;

        public Action CollectionChanged;

        public ObservableCollection<ISection> Items
        {
            get
            {
                ObservableCollection<ISection> lRet = new ObservableCollection<ISection>();
                foreach (ISection lCurr in mList)
                {
                    lRet.Add(lCurr.Clone());
                }
                return lRet;
            }
        }

        public int Count { get { return mList.Count; } }

        public SectionList()
        {
            mList = new ObservableCollection<ISection>();
        }

        public void Add(ISection aValue)
        {
            mList.Add(aValue);
        }

        public SectionList Clone()
        {
            SectionList lRet = new SectionList();
            lRet.mList = Items;
            return lRet;
        }
    }

    public MultiTypeDynamicTextBlock()
    {

    }

    public static readonly DependencyProperty ItemsCollectionProperty =
        DependencyProperty.Register("ItemsCollection", typeof(SectionList), typeof(MultiTypeDynamicTextBlock),
            new UIPropertyMetadata((PropertyChangedCallback)((sender, args) =>
            {
                MultiTypeDynamicTextBlock textBlock = sender as MultiTypeDynamicTextBlock;
                SectionList inlines = args.NewValue as SectionList;

                if (textBlock != null)
                {
                    if ((inlines != null) && (inlines.Count > 0))
                    {
                        textBlock.ItemsCollection.CollectionChanged += textBlock.ResetInlines;
                        textBlock.Inlines.Clear();
                        foreach (ISection lCurr in textBlock.ItemsCollection.Items)
                        {
                            textBlock.Inlines.Add(lCurr.GetDisplayElement());
                        }
                    }
                    else
                    {
                        inlines = new SectionList();
                        inlines.Add(new TextOption("No value set"));
                        textBlock.ItemsCollection = inlines;
                    }
                }
            })));

    public SectionList ItemsCollection
    {
        get
        {
            return (SectionList)GetValue(ItemsCollectionProperty);
        }
        set
        {
            SectionList lTemp;
            if (value == null)
            {
                lTemp = new SectionList();
                lTemp.Add(new TextOption("No value set for property"));
            }
            else
            {
                lTemp = value;
            }
            SetValue(ItemsCollectionProperty, lTemp);
        }
    }

    private void ResetInlines()
    {
        Inlines.Clear();
        foreach (ISection lCurr in ItemsCollection.Items)
        {
            Inlines.Add(lCurr.GetDisplayElement());
        }
    }
}

我将绑定的字段更新为 MultiTypeDynamicTextBlock.SectionList 类型

只要我使用副本(克隆)它就可以工作,由于某种原因,当我不克隆它时,它会从列表中的显示中删除值,如果有人知道我为什么想学习但我设法四处走动它。窗口的 XAML 是:

<StackPanel>
    <ListBox x:Name="TheList" ItemsSource="{Binding GeneralItems}" SelectedItem="{Binding SelectedItem}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Title}" FontSize="20"/>
                    <local:MultiTypeDynamicTextBlock ItemsCollection="{Binding Items}" Margin="20,0,0,0"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <StackPanel DataContext="{Binding GeneralItems, Path=SelectedItem}">
        <TextBlock Text="{Binding Title}" FontSize="20"/>
        <local:MultiTypeDynamicTextBlock DataContext="{Binding Items}" ItemsCollection="{Binding}" Margin="20,0,0,0"/>
    </StackPanel>
</StackPanel>
于 2013-10-27T10:32:41.300 回答
0

在您的示例中,是否SelectedItem有两个属性TitleItems?还是您的视图模型中Items的属性?如果答案是后者,那么您可以在下面找到解决方案。

我不完全明白你的意思,但我会试一试。如果您的意思ItemsSource是未设置您的自定义控件,那么您必须指向XAML正确的方向。如果这是您想要实现的目标,您可以在下面找到解决方案。我所做的是使用这行代码将编译器指向正确的源代码:

ItemsSource="{Binding DataContext.Items, RelativeSource={RelativeSource AncestorType=Window}}"

在这里,您说编译器可以Binding propertyDataContextWindow (或任何可以找到 的控件property)中找到 。

   <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 DataContext.Items, RelativeSource={RelativeSource AncestorType=Window}}"/>
        </StackPanel>
    </StackPanel>

希望这有帮助。


编辑

title property我从ListBox. 如果Items设置为 new ObservableCollection,您会在更改时调用OnPropertyChanged eventfor吗?ItemsSelectedItem

OnPropertyChanged("Items");
于 2013-10-23T16:03:44.113 回答