I need to put data from two different datasourses in the same textbox. The text that comes from the first one have to be bolded and the secound normal.
It's there a possibility to do this in WPF?
I need to put data from two different datasourses in the same textbox. The text that comes from the first one have to be bolded and the secound normal.
It's there a possibility to do this in WPF?
Hi! You cannot bind (or multibind) to Document
property of RichTextBox
, because it is NOT a DependencyProperty
(strange!!!)!!! See this link for a really easy way of subclassing RichTextBox
to create your own BindableRichTextBox
or this post for another workaround.
Now you can use MultiBinding
with a custom IMultiValueConverter
to achieve the results. Since you have not given much details of your problem, I can only give you an overall idea of what you should do:
<!--NOTE: Include xmlns:local=" .. " appropriately for your project-->
<Window.Resources>
<sys:String x:Key="SourceA">This text will be normal..</sys:String>
<sys:String x:Key="SourceB">This text will be Bold!!!</sys:String>
</Window.Resources>
And now you can do like this:
<local:BindableRichTextBox>
<!--<local:BindableRichTextBox.Document>-->
<MultiBinding Converter="{x:Static local:MySourceBToBoldConverter.Instance}">
<Binding Source="{StaticResource SourceA}" />
<Binding Source="{StaticResource SourceB}" />
</MultiBinding>
<!--</local:BindableRichTextBox.Document>-->
</local:BindableRichTextBox>
And then create a class MySourceBToBoldConverter
that inherits from IMultiValueConverter
like this:
public class MySourceBToBoldConverter : IMultiValueConverter
{
public static readonly MySourceBToBoldConverter Instance = new MySourceBToBoldConverter();
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//Now you'll get value from Source A as value[0]
// and value from Source B as value[1]
//Do whatever you want like bold etc...
//and return the result
string normalText = values[0] as string;
string boldText = values[1] as string;
Bold bold = new Bold();
bold.Inlines.Add(boldText);
Paragraph para = new Paragraph();
para.Inlines.Add(normalText);
para.Inlines.Add(bold);
FlowDocument rtbDocument = new FlowDocument();
rtbDocument.Blocks.Add(para);
return rtbDocument;
}
public object[] ConvertBack(object value, ... )
{
//Convert the object returned by Convert() back
//to its original form if it's possible;
//otherwise throw not supported exception ;)
throw new NotImplementedException();
}
}
Currently I don't have my work PC with me that has VS installed, so I can't give you a working example, but go ahead and search google/msdn/stackoverflow 4 MultiBinding
and IMultiValueConverter
and you'll find some good examples out there.
Check the working example here.
Regards,
Mihir Gokani