tags:

views:

53

answers:

1

Can I do something like this?

                <GroupBox.Header>
                    <GroupBox.Header.Resources>
                        <Style TargetType="Label">
                            <Setter Property="Foreground" Value="White"/>
                        </Style>
                    </GroupBox.Header.Resources>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="Realtime Event Viewer" VerticalAlignment="Center"/>
                        <Label Content="Watching: " Margin="20,0,0,0" VerticalAlignment="Center"/>
                        <Label Content="{Binding MonitorServerName}" VerticalAlignment="Center"/>
                    </StackPanel>
                </GroupBox.Header>
+3  A: 

You're setting the Header property of a GroupBox object to a StackPanel object. If you want resources available in the Header, add them to the Resources of the StackPanel object:

<GroupBox.Header>
    <StackPanel Orientation="Horizontal">
        <StackPanel.Resources>
            <Style TargetType="Label">
                <Setter Property="Foreground" Value="White"/>
            </Style>
        </StackPanel.Resources>
        <Label Content="Realtime Event Viewer" VerticalAlignment="Center"/>
        <Label Content="Watching: " Margin="20,0,0,0" VerticalAlignment="Center"/>
        <Label Content="{Binding MonitorServerName}" VerticalAlignment="Center"/>
    </StackPanel>
</GroupBox.Header>
Quartermeister
That's not I'm after. I won't necessarily have a stackpanel inside the header, it could be a different container or even just a label by itself.
Jonathan Allen
@Jonathan: I don't think I quite understand what you're trying to do, then. You can add to the Resources property of the GroupBox and the resources will be available in the Header because they will be in an ancestor in the visual tree. You can also add to the Resources property of the object being assigned to the Header property if it is anything that inherits from FrameworkElement or FrameworkContentElement. XAML has no syntax for setting a property of a property, so you need to set the property on the object as it is created. What are you trying to do that this syntax doesn't support?
Quartermeister
I want all labels in the group box header to be white. I don't want labels in the group box content to change. I eventually want to apply this to my resource file so if affects all group boxes.
Jonathan Allen
@Jonathan: Ah, that makes sense. Unfortunately, if a resource is not found within the object in the Header property, the next place it looks is the GroupBox, so anything outside the Header object would affect the Content as well. What if you created an inheritable attached property that identified elements within the Header of a GroupBox, and then used a Trigger in your style?
Quartermeister