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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With