Search code examples
wpfwpf-controlsmultibinding

How set different font color for different binding element


I am using Multibinding to set data in TextBlock. I want different color for different property binding.

Please see code for more details,

<StatusBar x:Name="messageBar">
      <StatusBarItem>
<TextBlock x:Name="txtStatusMessage" 
           TextWrapping="Wrap" Foreground="Red" Height="35">
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource ConvertMultiple}"
                      UpdateSourceTrigger="PropertyChanged">
            <Binding ElementName="txtUserFriendlyName" 
                     Path="(Validation.Errors)[0].ErrorContent"
                     UpdateSourceTrigger="PropertyChanged"/>

            <Binding ElementName="txtXPathValue" 
                     Path="(Validation.Errors)[0].ErrorContent" />

            <Binding ElementName="cboTagName" 
                     Path="(Validation.Errors)[0].ErrorContent" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

how can I give different font color for different Binding element.


Solution

  • Use different Runs inside textblock and give different color.

    <TextBlock x:Name="txtStatusMessage"
                TextWrapping="Wrap"
                Height="35">
        <Run Text="{Binding ElementName=txtUserFriendlyName, Path=(Validation.Errors)[0].ErrorContent}" Foreground="Red"/>
        <Run Text="{Binding ElementName=txtXPathValue, Path=(Validation.Errors)[0].ErrorContent}" Foreground="White"/>
        <Run Text="{Binding ElementName=cboTagName, Path=(Validation.Errors)[0].ErrorContent}" Foreground="Green"/>
    </TextBlock>