Search code examples
c#wpfxamlsolidcolorbrush

Trying to apply SolidColorBrush resource to HeaderedContentControl BorderBrush


I'd like to link a SolidColorBrush from my Window to Another SolidColorBrush in My dictionary. I didn't find something like this , and may be it's not possible ...

here is the code in my "ResourceDictionary.xaml"

<SolidColorBrush x:Key="BrushBlueTransparent" Color="#33006D8F"/>

And in my windows i want a link to this resource like this :

<SolidColorBrush x:Key="ControlColor" Color="{Binding Source={DynamicResource BrushEvasanOrange}}"/>

For now, this code don't work ...

I want to use this link because i want to use this resource in my page in a multiple "" and if the color had to be change in the futur it could be easy to change with this way.

The Brush resource is used like this:

<HeaderedContentControl 
    x:Name="_demandeur" 
    BorderBrush="{DynamicResource BrushEncadre}" 
    BorderThickness="1" 
    Padding="10" 
    Margin="0,20,0,0" 
    Header="{x:Static p:Resources.EV_Demandeur}"
    >
    <WrapPanel 
        Margin="0" 
        Orientation="Horizontal" 
        HorizontalAlignment="Left"
        >
        <TextBlock 
            TextWrapping="Wrap" 
            FontWeight="Normal" 
            Text="text"
            />
    </WrapPanel>
</HeaderedContentControl>

Solution

  • It sounds like your problem is that HeaderedContentControl ignores its BorderBrush property. There are two ways to fix this: One is to replace the HeaderedContentControl's Template with one that displays a border around the content, but that's a lot of trouble. Another is to use a subclass of HeaderedContentControl which already has a template that does you want (we'll get to that last).

    One very simple option is to simply put a Border around the control, and move the Margin to the Border as well, so the orange border line will be inside the margin. This isn't the right answer in your specific case, but it's a good general answer to "how do I put a border around things in XAML?"

    <Border
        BorderBrush="{StaticResource BrushEncadre}" 
        BorderThickness="1" 
        Margin="0,20,0,0" 
        >
        <HeaderedContentControl 
            x:Name="_demandeur" 
            Padding="10" 
            Header="{x:Static p:Resources.EV_Demandeur}"
            >
            <WrapPanel 
                Margin="0" 
                Orientation="Horizontal" 
                HorizontalAlignment="Left" >
                <TextBlock 
                    TextWrapping="Wrap" 
                    FontWeight="Normal" 
                    Text="text"
                    />
            </WrapPanel>
        </HeaderedContentControl>
    </Border>
    

    But I'm wondering if HeaderedContentControl is really what you want here. HeaderedContentControl is a base class for a variety of controls which display content with a header. The subclasses are much more commonly used, and I have a feeling that what you really want here is GroupBox, which is one of those subclasses. You'd use it just the way you were using HeaderedContentControl:

    <GroupBox 
        x:Name="_demandeur" 
        Padding="10" 
        Margin="0,20,0,0" 
        Header="{x:Static p:Resources.EV_Demandeur}"
        BorderBrush="{StaticResource BrushEncadre}" 
        BorderThickness="1" 
        >
        <WrapPanel 
            Margin="0" 
            Orientation="Horizontal" 
            HorizontalAlignment="Left" >
            <TextBlock 
                TextWrapping="Wrap" 
                FontWeight="Normal" 
                Text="text"
                />
        </WrapPanel>
    </GroupBox>