1
votes

I created a new TextBlock class which has ItemsSource property and translates that ItemsSource into "Run" object:

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());
            }
        }
    }

If I Bind the ItemsSource directly to the DataContext, it works. But if I bind it to an object that changes at runtime (such as SelectedItem on a ListBox) it doesn't update the text when a new item is selected.

<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>

Any reason why?

2
what do you mean by "If I Bind the ItemsSource directly to the DataContext, it works."? and the example of xaml you gave in question is it the working or non working example?Nitin
Add this ItemsSource="{Binding}"Aravindan Srinivasan
What is the type of the objects in the Items collection?Loetn
Nit - This example is not working. Aravind - Where do you want me to add this, the MultiTypeDynamicTextBlock already has an ItemsSource so I can't give it another and changing the binding will point it to another location Loetn - Title is of type string and Items is of type ObservableCollection<MultiTypeDynamicTextBlock.ISection>user2342987

2 Answers

0
votes

In your example, does the SelectedItem has two properties Title and Items? Or is Items a property in your viewmodel? If the answer is the latter, than you can find a solution below.

I don't entirely understand what you mean, but I'll give it a try. If you mean that the ItemsSource on your custom control isn't set, than you have to point XAML into the right direction. Below you can find a solution, if this is what you want to achieve. What I did is pointing the compiler to the right source with this line of code:

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

Here you say that the compiler can find the Binding property in the DataContext of the Window (or any control where you can find the 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>

Hopefully this helped.


EDIT

The title property will changes when I select another one from the ListBox. If Items is set to a new ObservableCollection, do you call the OnPropertyChanged event for Items when the SelectedItem changes?

OnPropertyChanged("Items");
0
votes

Thank you for your help. I managed to fix this by updating the MultiTypeDynamicTextBlock as follows:

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());
        }
    }
}

And I update the fields that were Binded to be of type MultiTypeDynamicTextBlock.SectionList

As long as I am using a copy (Clone) it is working, for some reason when I don't clone it removes the value from the display in the list, if someone knows why I would love to learn but I managed to go around it. the XAML of the window is:

<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>