Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8724343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:48:36+00:00 2026-06-13T07:48:36+00:00

I built a xaml class and then realized that if I wanted to use

  • 0

I built a xaml class and then realized that if I wanted to use it as an abstract base class that it had to be built without xaml. But when I got done converting my xaml to C#, the result didn’t output the same.

E.g. The foreground is not the same. (ViewDialogX on left, ViewDialogC on right)

xaml vs c# instances

ViewDialogX.xaml

<ContentControl x:Class="xmlns.ViewDialogX"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:my="clr-namespace:xmlns"
             HorizontalAlignment="Center" VerticalAlignment="Center">
    <ContentControl.Template>
        <ControlTemplate TargetType="my:ViewDialogX">
            <Border MinWidth="160" MinHeight="90" Margin="1,2,3,3" Padding="4">
                <Border.Effect>
                    <DropShadowEffect BlurRadius="10" Opacity="3" ShadowDepth="0" />
                </Border.Effect>
                <GroupBox Header="{TemplateBinding Title}" Background="{DynamicResource ControlBackgroundBrush}">
                    <Grid Width="{TemplateBinding ContentWidth}" Height="{TemplateBinding ContentHeight}">
                        <ContentPresenter Margin="{TemplateBinding Padding}" Content="{TemplateBinding Content}" />
                    </Grid>
                </GroupBox>
            </Border>
        </ControlTemplate>
    </ContentControl.Template>
</ContentControl>

ViewDialogX.xaml.cs

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Markup;

namespace xmlns
{
    public partial class ViewDialogX : ContentControl
    {
        public ViewDialogX()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.RegisterAttached("Title", typeof(string), typeof(ViewDialogX), new PropertyMetadata(" "));
        public string Title { get { return (string)this.GetValue(TitleProperty); } set { this.SetValue(TitleProperty, value); } }

        public static readonly DependencyProperty ContentWidthProperty =
            DependencyProperty.RegisterAttached("ContentWidth", typeof(double), typeof(ViewDialogX), new PropertyMetadata(double.NaN));
        public double ContentWidth { get { return (double)this.GetValue(ContentWidthProperty); } set { this.SetValue(ContentWidthProperty, value); } }

        public static readonly DependencyProperty ContentHeightProperty =
            DependencyProperty.RegisterAttached("ContentHeight", typeof(double), typeof(ViewDialogX), new PropertyMetadata(double.NaN));
        public double ContentHeight { get { return (double)this.GetValue(ContentHeightProperty); } set { this.SetValue(ContentHeightProperty, value); } }

        public virtual void OnClosing(CancelEventArgs e) { }
        public virtual void Close() { }
    }
}

ViewDialogC.cs

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Markup;

namespace xmlns
{
    public class ViewDialogC : ContentControl
    {
        public ViewDialogC()
        {
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                this.SetValue(ViewDialogC.HorizontalAlignmentProperty, HorizontalAlignment.Center);
                this.SetValue(ViewDialogC.VerticalAlignmentProperty, VerticalAlignment.Center);

                FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));
                border.SetValue(Border.MinWidthProperty, 160d);
                border.SetValue(Border.MinHeightProperty, 90d);
                border.SetValue(Border.MarginProperty, new Thickness(1, 2, 3, 3));
                border.SetValue(Border.PaddingProperty, new Thickness(4));
                border.SetValue(Border.EffectProperty, new DropShadowEffect() { BlurRadius = 10, Opacity = 3, ShadowDepth = 0 });
                FrameworkElementFactory group = new FrameworkElementFactory(typeof(GroupBox));
                group.SetValue(GroupBox.HeaderProperty, new TemplateBindingExtension(ViewDialogC.TitleProperty));
                group.SetResourceReference(GroupBox.BackgroundProperty, "ControlBackgroundBrush");
                FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid));
                grid.SetValue(Grid.WidthProperty, new TemplateBindingExtension(ViewDialogC.ContentWidthProperty));
                grid.SetValue(Grid.HeightProperty, new TemplateBindingExtension(ViewDialogC.ContentHeightProperty));
                FrameworkElementFactory content = new FrameworkElementFactory(typeof(ContentPresenter));
                content.SetValue(ContentPresenter.MarginProperty, new TemplateBindingExtension(ViewDialogC.PaddingProperty));
                content.SetValue(ContentPresenter.ContentProperty, new TemplateBindingExtension(ViewDialogC.ContentProperty));
                grid.AppendChild(content);
                group.AppendChild(grid);
                border.AppendChild(group);
                this.SetValue(ViewDialogC.TemplateProperty, new ControlTemplate(typeof(ViewDialogC)) { VisualTree = border });
            }
        }

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.RegisterAttached("Title", typeof(string), typeof(ViewDialogC), new PropertyMetadata(" "));
        public string Title { get { return (string)this.GetValue(TitleProperty); } set { this.SetValue(TitleProperty, value); } }

        public static readonly DependencyProperty ContentWidthProperty =
            DependencyProperty.RegisterAttached("ContentWidth", typeof(double), typeof(ViewDialogC), new PropertyMetadata(double.NaN));
        public double ContentWidth { get { return (double)this.GetValue(ContentWidthProperty); } set { this.SetValue(ContentWidthProperty, value); } }

        public static readonly DependencyProperty ContentHeightProperty =
            DependencyProperty.RegisterAttached("ContentHeight", typeof(double), typeof(ViewDialogC), new PropertyMetadata(double.NaN));
        public double ContentHeight { get { return (double)this.GetValue(ContentHeightProperty); } set { this.SetValue(ContentHeightProperty, value); } }

        public virtual void OnClosing(CancelEventArgs e) { }
        public virtual void Close() { }
    }
}

Implementation xaml code:

    <my:ViewDialogX Title="Test">
        <Button Content="Button" />
    </my:ViewDialogX>
    <my:ViewDialogC Title="Test">
        <Button Content="Button" />
    </my:ViewDialogC>

Extra Styles:

<SolidColorBrush x:Key="ControlBackgroundBrush" Color="#333333" />
<SolidColorBrush x:Key="NormalBackgroundBrush" Color="#595959" />
<LinearGradientBrush x:Key="ShineBrush" StartPoint="0,0.042" EndPoint="0,0.971">
    <GradientStop Color="#59FFFFFF" Offset="0" />
    <GradientStop Color="#26FFFFFF" Offset="0.467" />
    <GradientStop Color="#00FFFFFF" Offset="0.475" />
</LinearGradientBrush>
<LinearGradientBrush x:Key="HoverShineBrush" StartPoint="0,0" EndPoint="0,1">
    <GradientStop Color="#4CFFFFFF" Offset="0" />
    <GradientStop Color="#26FFFFFF" Offset="0.467" />
    <GradientStop Color="#10FFFFFF" Offset="0.475" />
    <GradientStop Color="#10FFFFFF" Offset="0.856" />
    <GradientStop Color="#26FFFFFF" Offset="1" />
</LinearGradientBrush>
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#ADADAD" />
<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#787878" />
<SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA" />
<Style TargetType="{x:Type GroupBox}" BasedOn="{x:Null}">
    <Setter Property="Background" Value="{x:Null}" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="BorderBrush" Value="{DynamicResource ControlBackgroundBrush}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Padding" Value="3" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupBox}">
                <DockPanel>
                    <Grid x:Name="HeaderGrid" DockPanel.Dock="Top">
                        <Border x:Name="BackgroundElement" Background="{DynamicResource NormalBackgroundBrush}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3" />
                        <Border x:Name="ShineElement" Background="{DynamicResource ShineBrush}" BorderThickness="0" Margin="1" CornerRadius="3" />
                        <Border x:Name="ShineBorderElement" BorderBrush="{DynamicResource HoverShineBrush}" BorderThickness="1" CornerRadius="2" Margin="{TemplateBinding BorderThickness}" />
                        <ContentPresenter x:Name="ContentElement" SnapsToDevicePixels="True" ContentSource="Header"  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="4" RecognizesAccessKey="True" />
                    </Grid>
                    <Border DockPanel.Dock="Bottom" Margin="0,-1,0,0" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3">
                        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="{TemplateBinding Padding}" />
                    </Border>
                </DockPanel>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}" />
                        <Setter Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="BackgroundElement" />
                        <Setter Property="BorderBrush" Value="{DynamicResource DisabledBorderBrush}" TargetName="ShineBorderElement" />
                        <Setter Property="Opacity" TargetName="HeaderGrid" Value="0.75" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Once again my question is: why isn’t the output of ViewDialogX and ViewDialogC the same?

(Somehow ViewDialogC is editing the foreground of the groupbox.)

Edit:

I’ve found the cause of the foreground change on this line:

group.SetResourceReference(GroupBox.BackgroundProperty, "ControlBackgroundBrush");

(See ViewDialogX.xaml for what I am trying to achieve)

I can hard set the background to be a specific color or even bind it to a template parent property, but when the FrameworkElementFactory gets a dynamic resource type of property, then the foreground gets inherited from the TemplateParent. Is this a bug?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:48:38+00:00Added an answer on June 13, 2026 at 7:48 am

    I have contacted the Microsoft WPF Development Team. They acknowledge this as a bug, but have prioritised it as low and not likely to be fixed.

    My work around for this example: Use another control to take the *.SetResourceReference call and draw the background, instead of the GroupBox.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I built a website and then deploy it on the server. But when i
I'm creating a class library that makes available some XAML windows (in theory). To
I want to create a custom control that extends a built-in control and then
I built a little view locator. public abstract class ViewModelLocatorBase : IViewModelLocator { private
I've got a custom Button class, that always performs the same action when it
I've built a SpinButton user control. SpinButton.xaml has: <UserControl x:Class=MyApp.SpinButton x:Name=Spinner [...] > <Grid
I have a Silverlight application that is built from a set of Silverlight class
Ive built a little jquery sliding div on my website that when you hover
I built a linear layout inside of a dialog , the problem here that
How do I add .CS functionality to XAML classes built in Expression Design? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.