VS2008 / net 3.0 / wpf

  • Thread starter Thread starter Rolf Welskes
  • Start date Start date
R

Rolf Welskes

Hello,
because there are no manged news groups for windows presentation foundation,
I was told to
send my question here.

Problem:
Look at the following xaml:

<Grid Background="Beige" Margin="84,79,215,83" Name="grid01"
TextBlock.FontSize="24.0">
<Button Height="23" HorizontalAlignment="Left" Margin="20,22,0,0"
Name="button1" VerticalAlignment="Top" Width="75">Button</Button>
</Grid>

I set over the attached property FontSize the FontSize for the grid (which
as it's own has no fontsize.).
Now this fontsize is inherented to all childs, in this sample to the button.
This works fine.

Now the same with Foreground:
<Grid Background="Beige" Margin="84,79,215,83" Name="grid01"
TextBlock.Foreground="Red">
<Button Height="23" HorizontalAlignment="Left" Margin="20,22,0,0"
Name="button1" VerticalAlignment="Top" Width="75">Button</Button>
</Grid>

It's all the same, Foreground also is an inherentable DependencyProperty.

BUT: this does not work.

So, It's not possible to set forground for all elements in a grid.

What is the reason.

Thank you for any help.

Best Regards
Rolf Welskes
 
Rolf Welskes said:
Hello,
because there are no manged news groups for windows presentation
foundation,
I was told to
send my question here.

Problem:
Look at the following xaml:

<Grid Background="Beige" Margin="84,79,215,83" Name="grid01"
TextBlock.FontSize="24.0">
<Button Height="23" HorizontalAlignment="Left" Margin="20,22,0,0"
Name="button1" VerticalAlignment="Top" Width="75">Button</Button>
</Grid>

I set over the attached property FontSize the FontSize for the grid (which
as it's own has no fontsize.).
Now this fontsize is inherented to all childs, in this sample to the
button.
This works fine.

Now the same with Foreground:
<Grid Background="Beige" Margin="84,79,215,83" Name="grid01"
TextBlock.Foreground="Red">
<Button Height="23" HorizontalAlignment="Left" Margin="20,22,0,0"
Name="button1" VerticalAlignment="Top" Width="75">Button</Button>
</Grid>

It's all the same, Foreground also is an inherentable DependencyProperty.

BUT: this does not work.

So, It's not possible to set forground for all elements in a grid.

What is the reason.

I don't fully understand this myslef, but I think that what is happening is
that the TextBlock inside the Button inherits its Foreground property from
the Button's Foreground property, which is actually a Control.Foreground
property. The Control.Foreground property is defined in the static Control
constructor as:
ForegroundProperty =
TextElement.ForegroundProperty.AddOwner(typeof(Control), new
FrameworkPropertyMetadata(SystemColors.ControlTextBrush,
FrameworkPropertyMetadataOptions.Inherits));

Thus Control is adding itself as an owner of the TextElement.Foreground
property (which is the attached property that you are setting in the "<Grid"
XAML, as TextBlock.ForegroundProperty is inherited from
TextElement.ForegroundProperty), and setting the default value to be
SystemColors.ControlTextBrush. According to MSDN, adding an owner to an
attached property effectively makes that property a non-attached dependency
property of the new owner. The actual explanation from
http://msdn.microsoft.com/en-us/library/ms752375.aspx is: "You can call
AddOwner for a dependency property that is defined as an attached property
by the owner class. Generally the reason for doing this is to expose the
previously attached property as a non-attached dependency property. You then
will expose the AddOwner return value as a public static readonly field for
use as the dependency property identifier, and will define appropriate
"wrapper" properties so that the property appears in the members table and
supports a non-attached property usage in your class."

This seems to explain why the TextBox within the Button does not take its
Foreground colour from the value set in the "<Grid" element - although I
have no idea why Microsoft chose to do things that way.

I don't know of a simple solution that will allow you to do what you want.
You could add something like the following in <Grid.Resources>

<Style TargetType="Button">
<Setter Property="Foreground" Value="{Binding
Path=(TextElement.Foreground), RelativeSource={RelativeSource
AncestorType={x:Type Grid}}}" />
</Style>

but you'd also have to add a similar style for other controls (if you used
them) such as CheckBox, RadioButton, etc.

Chris Jobson
 
Back
Top