This XAML will work fine, but its limiting (it won't allow you to set the color of the text displayed, as requested):
<dxb:BarStaticItem
Content="{Binding MyStatusBarText}">
</dxb:BarStaticItem>
This particular control does allow us to set the ContentTemplate
. We can use this to style the content:
<dxb:BarStaticItem
ContentTemplate="????">
</dxb:BarStaticItem>
First, we define a DataTemplate
in Window.Resources
. This is what our ContentTemplate
will point at:
<Window.Resources>
<DataTemplate x:Key="MyStatusBarContentTemplate">
<!-- As the DataContext of a resource does not default to the window, we have to use RelativeSource to find the window. -->
<TextBlock Name="MyText"
Text="{Binding Path=MyStatusBarText,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=Window}}">
</TextBlock>
</DataTemplate>
</Window.Resources>
As the DataContext
of a DataTemplate
is different to the rest of the XAML, if we omit the XAML RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}
then the binding will not work properly. Other than that, all we are doing is defining a template which can be used to render the contents of the control.
Then, we point our control at this DataTemplate
:
<dxb:BarStaticItem
ContentTemplate="{DynamicResource MyStatusBarContentTemplate}">
</dxb:BarStaticItem>
Now that we have defined a custom data template, we can do anything we want. For example, we could add a Converter
which colored the text red if the status bar contained the text Error
(something that was impossible, otherwise).
This answer also illustrates how it is possible to use a DataTemplate
to display custom content for most controls.
Update
Rather than defining the DataTemplate
in the resources for the Window, defined it as a resource for BarStaticItem
. This keeps related items together in the XAML.
This particular XAML means that the status bar text automatically goes red if the text contains the string Error
, and the status bar text is automatically prefixed with the time. Let me know if you want me to post the C# code for the converters.
<dxb:BarStaticItem
ContentTemplate="{DynamicResource MyStatusBarContentTemplate}">
<dxb:BarStaticItem.Resources>
<DataTemplate x:Key="MyStatusBarContentTemplate">
<!-- As the DataContext of a resource does not default to the window, we have to use RelativeSource to find the window. -->
<TextBlock Name="MyText"
Foreground="{Binding ElementName=MyText, Path=Text, Converter={StaticResource ColorRedIfTextContainsError}}"
Text="{Binding Path=SettingsGlobalViewModel.StatusBarText,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=Window},
Converter={StaticResource PrefixStringWithTime}}">
</TextBlock>
</DataTemplate>
</dxb:BarStaticItem.Resources>
</dxb:BarStaticItem>