0
votes

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
How exactly does it make any difference if you define the controls in XAML or C#? If there is a difference then you have done something wrong. Also UserControls are never meant to be inherited.H.B.
When XAML is turned into C# code something can happen that makes a difference. Non inheriting is not an option.Ignacio Soler Garcia
It surely is an option as you are shooting yourself in the foot if you stick to a UserControl when you need a custom control.H.B.
Why I'm shooting myself at the foot?Ignacio Soler Garcia
UserControls are not meant to provide this kind of functionality, and having having to deal with C# such as the above is the very definition of shooting oneself in the foot.H.B.

1 Answers

1
votes

@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.