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 6948335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:48:34+00:00 2026-05-27T13:48:34+00:00

I had a working user control done in WPF in XAML. Now I have

  • 0

I had a working user control done in WPF in XAML. Now I have removed the XAML and created the usercontrol with code because I need to inherit from this control. Previosuly I had code like this one on the code using the control:

<mc:MyControl Foreground="White">

And all the controls inside the MyControl control used this Foreground setting. Now that I’ve created the control just using C# this is not happening anymore.

Someone know why and how to fix this?

Thanks in advance.

EDIT:

This is the XAML:

<UserControl x:Class="Utils.Wpf.Chart.Chart"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         SizeChanged="UserControl_SizeChanged">
<Grid>
    <Border Margin="5" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <Canvas Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="0" Grid.RowSpan="2"  x:Name="DrawArea" Canvas.Background="Transparent" MouseEnter="DrawArea_MouseEnter" MouseLeave="DrawArea_MouseLeave" MouseMove="DrawArea_MouseMove" Cursor="None">
                <Line x:Name="Border1" X1="0" X2="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}}" Y1="{Binding Path=ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}}" Y2="{Binding Path=ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}}" StrokeThickness="1" Stroke="{Binding BorderBrush, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/>
                <Line x:Name="Border2" Y1="0" Y2="{Binding Path=ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}}" X1="0" X2="0" StrokeThickness="1" Stroke="{Binding BorderBrush, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/>
                <TextBlock x:Name="CrossValue" Text="[-,-]" Foreground="#EBDE11" Visibility="Hidden"/>
            </Canvas>

            <TextBlock Grid.Column="0" Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Right" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MaxYCaption, TargetNullValue=MaxY}" Margin="3,0,6,0"/>
            <TextBlock Grid.Column="0" Grid.Row="1" VerticalAlignment="Bottom" HorizontalAlignment="Right" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MinYCaption, TargetNullValue=MinY}" Margin="3,0,6,0"/>

            <TextBlock Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Top" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MinX, TargetNullValue=MinX}" Margin="3,3,0,0"/>
            <TextBlock Grid.Column="2" Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Top" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MaxX, TargetNullValue=MaxX}" Margin="0,3,3,0"/>
        </Grid>
    </Border>
</Grid>
</UserControl>

This is the C#:

        _outerGrid = new Grid();
        _border = new Border();
        _innerGrid = new Grid();
        _canvas = new Canvas();
        _line1 = new Line();
        _line2 = new Line();
        _cursorText = new TextBlock();
        _maxXText = new TextBlock();
        _maxYText = new TextBlock();
        _minXText = new TextBlock();
        _minYText = new TextBlock();

        this.Content = _outerGrid;

        _border.Margin = new Thickness(5);
        _border.HorizontalAlignment = HorizontalAlignment.Stretch;
        _border.VerticalAlignment = VerticalAlignment.Stretch;

        _outerGrid.Children.Add(_border);

        _column1 = new ColumnDefinition();
        _column1.Width = new GridLength(0, GridUnitType.Auto);
        _column2 = new ColumnDefinition();
        _column2.Width = new GridLength(1, GridUnitType.Star);
        _column3 = new ColumnDefinition();
        _column3.Width = new GridLength(1, GridUnitType.Star);

        _row1 = new RowDefinition();
        _row1.Height = new GridLength(1, GridUnitType.Star);
        _row2 = new RowDefinition();
        _row2.Height = new GridLength(1, GridUnitType.Star);
        _row3 = new RowDefinition();
        _row3.Height = new GridLength(0, GridUnitType.Auto);

        _innerGrid.ColumnDefinitions.Add(_column1);
        _innerGrid.ColumnDefinitions.Add(_column2);
        _innerGrid.ColumnDefinitions.Add(_column3);

        _innerGrid.RowDefinitions.Add(_row1);
        _innerGrid.RowDefinitions.Add(_row2);
        _innerGrid.RowDefinitions.Add(_row3);

        _border.Child = _innerGrid;

        _canvas.Background = Brushes.Transparent;
        _canvas.Cursor = Cursors.None;
        _canvas.MouseEnter += new MouseEventHandler(_canvas_MouseEnter);
        _canvas.MouseMove += new MouseEventHandler(_canvas_MouseMove);
        _canvas.MouseLeave += new MouseEventHandler(_canvas_MouseLeave);

        Grid.SetColumn(_canvas,1);
        Grid.SetColumnSpan(_canvas,2);
        Grid.SetRowSpan(_canvas,2);

        _innerGrid.Children.Add(_canvas);

        _line1.X1 = 0;
        _line1.StrokeThickness = 1;

        Binding x2Binding = new Binding("ActualWidth");
        x2Binding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) {AncestorType = _canvas.GetType()};
        _line1.SetBinding(Line.X2Property, x2Binding);

        Binding yBinding = new Binding("ActualHeight");
        yBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = _canvas.GetType() };
        _line1.SetBinding(Line.Y1Property, yBinding);
        _line1.SetBinding(Line.Y2Property, yBinding);

        _line2.Y1 = 0;
        _line2.X1 = 0;
        _line2.X2 = 0;
        _line2.StrokeThickness = 1;

        Binding y2Binding = new Binding("ActualHeight");
        y2Binding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = _canvas.GetType() };
        _line2.SetBinding(Line.Y2Property, x2Binding);

        Binding strokeBinding = new Binding("BorderBrush");
        strokeBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(UserControl) };
        _line2.SetBinding(Shape.StrokeProperty, strokeBinding);

        _cursorText.Text = "[-,-]";
        _cursorText.Foreground = new SolidColorBrush(Color.FromRgb(0xeb, 0xde, 11));
        _cursorText.Visibility = Visibility.Hidden;

        _canvas.Children.Add(_line1);
        _canvas.Children.Add(_line2);
        _canvas.Children.Add(_cursorText);

        _maxYText.VerticalAlignment = VerticalAlignment.Top;
        _maxYText.HorizontalAlignment = HorizontalAlignment.Right;
        _maxYText.Margin = new Thickness(3, 0, 6, 0);

        Binding maxYBinding = new Binding("MaxYCaption");
        maxYBinding.TargetNullValue = "MaxY";
        maxYBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(UserControl) };
        _maxYText.SetBinding(TextBlock.TextProperty, maxYBinding);

        _minYText.VerticalAlignment = VerticalAlignment.Bottom;
        _minYText.HorizontalAlignment = HorizontalAlignment.Right;
        _minYText.Margin = new Thickness(3, 0, 6, 0);

        Binding minYBinding = new Binding("MinYCaption");
        minYBinding.TargetNullValue = "MinY";
        minYBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(UserControl) };
        _minYText.SetBinding(TextBlock.TextProperty, minYBinding);

        _maxXText.VerticalAlignment = VerticalAlignment.Top;
        _maxXText.HorizontalAlignment = HorizontalAlignment.Right;
        _maxXText.Margin = new Thickness(0, 3, 3, 0);

        Binding maxXBinding = new Binding("MaxX");
        maxXBinding.TargetNullValue = "MaxX";
        maxXBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(UserControl) };
        _maxXText.SetBinding(TextBlock.TextProperty, maxXBinding);

        _minXText.VerticalAlignment = VerticalAlignment.Top;
        _minXText.HorizontalAlignment = HorizontalAlignment.Left;
        _minXText.Margin = new Thickness(3, 3, 0, 0);

        Binding minXBinding = new Binding("MinX");
        minXBinding.TargetNullValue = "MinX";
        minXBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(UserControl) };
        _minXText.SetBinding(TextBlock.TextProperty, minXBinding);

        _innerGrid.Children.Add(_maxYText);
        _innerGrid.Children.Add(_maxXText);
        _innerGrid.Children.Add(_minYText);
        _innerGrid.Children.Add(_minXText);

        Grid.SetColumn(_maxXText, 2);
        Grid.SetColumn(_minXText, 1);
        Grid.SetRow(_minYText, 1);
        Grid.SetRow(_minXText, 2);
        Grid.SetRow(_maxXText, 2);

        this.SizeChanged += new System.Windows.SizeChangedEventHandler(Chart2_SizeChanged);
  • 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-05-27T13:48:34+00:00Added an answer on May 27, 2026 at 1:48 pm

    @SoMoS – I think what you are talking about is a CustomControl, and not a UserControl. If so, did you apply a control template to your inherited control?

    You can read about the differences between custom controls to user controls in here-

    http://wangmo.wordpress.com/2007/09/28/user-controls-vs-custom-controls/

    http://www.wpftutorial.net/CustomVsUserControl.html

    If you will apply a control template to your custom control, you could manage its look just as you would with a User Control with XAML.

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

Sidebar

Related Questions

I have a WPF user control for which I need to force rendering in
I had been working on server side(c#) for a couple of years. But now
I have two instances of a File Browser user control, the control is just
I have one sharepoint custom page application which is rendering from a user control.
A while back I wrote a silverlight user control which had a csv import/export
I would like to show my user control inside row details. I have a
I have an ASP.NET TextBox ID=txtDate in my usercontrol. It has ValidationGroup=MyUC set. Now
I was working on a WPF project today that had a main nav window
I have a UserControl that contains two ContentControls that need to have different UserControl
I have a basic TextBox on a user control I am dynamically loading. 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.