3

Is it possible to use ValueConverter with Inlines? I need some parts of the every line in a ListBox bolded.

<TextBlock>
  <TextBlock.Inlines>
     <MultiBinding Converter="{StaticResource InlinesConverter}">
        <Binding RelativeSource="{RelativeSource Self}" Path="FName"/>
     </MultiBinding>
  </TextBlock.Inlines>  
</TextBlock>

It compiles but I get: A 'MultiBinding' cannot be used within a 'InlineCollection' collection. A 'MultiBinding' can only be set on a DependencyProperty of a DependencyObject.

If that is not possible, what approach would you suggest to be able to pass the whole TextBlock to IValueConverter?

David Daks
  • 139
  • 1
  • 1
  • 9

3 Answers3

5

As someone else pointed out, Inlines is not a Dependency Property which is why you are getting that error. When I've run across similar situations in the past, I've found Attached Properties/Behaviors to be a good solution. I'm going to assume that FName is of type string, and consequently so is the attached property. Here's an example:

class InlinesBehaviors
{
    public static readonly DependencyProperty BoldInlinesProperty = 
        DependencyProperty.RegisterAttached(
            "BoldInlines", typeof(string), typeof(InlinesBehaviors), 
            new PropertyMetadata(default(string), OnBoldInlinesChanged));

    private static void OnBoldInlinesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var textBlock = d as TextBlock;
        if (textBlock != null) 
        {
            string fName = (string) e.NewValue; // value of FName
            // You can modify Inlines in here
            ..........
        }
    }

    public static void SetBoldInlines(TextBlock element, string value)
    {
        element.SetValue(BoldInlinesProperty, value);
    }

    public static string GetBoldInlines(TextBlock element)
    {
        return (string) element.GetValue(BoldInlinesProperty);
    }
}

You could then use this in the following way (my is the xml namespace pointing to the namespace containing the AttachedProperty):

<TextBlock my:InlinesBehaviors.BoldInlines="{Binding FName}" />

The binding above could have been a multi binding, you could have used a converter, or whatever. Attached Behaviors are pretty powerful and this seems like a good place to use them.

Also, in case you're interested, here's an article on Attached Behaviors.

denis morozov
  • 6,236
  • 3
  • 30
  • 45
Tejas Sharma
  • 3,420
  • 22
  • 35
  • I have several errors on "public static readonly DependencyProperty BoldInlinesProperty = DependencyProperty.RegisterAttached( "BoldInlines", typeof(string), typeof("InlinesBehaviors"), new PropertyMetadata(default(string), OnBoldInlinesChanged));" – David Daks May 24 '13 at 16:23
  • I got it, "InlinesBehaviors" should be without quotation marks. – David Daks May 24 '13 at 17:09
  • Thank you very much! I have seen many posts mentioning it is not possible and not event a quick&dirty solutions were suggested. I knew someone must know the solution. – David Daks May 24 '13 at 17:58
  • I only just saw your comments, I'm glad you were able to figure out my typo and that I could help. – Tejas Sharma May 24 '13 at 22:32
4

I ran across this question while searching for a solution to the same error of "A 'MultiBinding' cannot be used within a 'InlineCollection' collection". My problem was not with the InLines property, but I had inadvertently left out the <TextBlock.Text> tag when defining my MultiBinding for my TextBlock. Just something to check if you're also getting this error.

crosstec
  • 302
  • 3
  • 9
0

No, Inlines is not a DependencyProperty - that's basically what the error message is saying.

See here for possible solutions.

Community
  • 1
  • 1
Tim Rogers
  • 21,297
  • 6
  • 52
  • 68
  • That makes sense. This is also beginner's question, but is there a way to bold numbers in a TextBlock's Text with datatriggers? – David Daks May 24 '13 at 15:13