Click here to Skip to main content
11,789,706 members (63,383 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: WPF
Hello guys, greetings to all and please I sincerely hope you can help me, this little problem solving will serve me something more, see:
I have a user control in which a small horizontal line drawing that emulates the X axis
And below it, drawing their numbers, something simple.
In trying to change the attributes of that control via XAML from the main window, failed to update the graph and display the new values, but not something that is failing in my dependency properties, please help.
The code is very readable only have a little patience please.
<UserControl x:Class="WpfPruebaUserControl.UserControl_EjeX"
             Etc >
    <Viewbox Stretch="Fill">
        <Canvas Height="200" Width="400" Margin="2" Name="CanvasAreaDibujo" />
public partial class UserControl_EjeX : UserControl
        double aNroDivX = 10d; 
        double aSepMarcasX;  
        double aGrosorLinea = 2d;
        Brush  aColorNumeros = new SolidColorBrush(Color.FromRgb(238, 201, 0));
        public UserControl_EjeX()
            DibujarEjeX (CanvasAreaDibujo);

        public void DibujarEjeX(Canvas MyCanvas)
            Line EjeX = new Line() 
                Stroke = new SolidColorBrush(Colors.Navy),
                StrokeThickness = aGrosorLinea,
                X1 = 20,
                Y1 = (MyCanvas.Height / 2.0f),
                X2 = MyCanvas.Width - 20,
                Y2 = (MyCanvas.Height / 2.0f),

            aSepMarcasX = (MyCanvas.Width) / aNroDivX;
            double k = 0d;
            for (double i = 20; i <= MyCanvas.Width - 20; i += aSepMarcasX)
                TextBlock tb = new TextBlock() { Text = (k).ToString(),  Foreground = aColorNumeros, FontFamily = new FontFamily("Arial"), FontSize = 9, FontWeight = FontWeights.Bold };
                Canvas.SetLeft(tb, i - 2);
                Canvas.SetTop(tb, (MyCanvas.Height / 2.0f) + 10);

        public static DependencyProperty NroDivXProperty = DependencyProperty.Register("NroDivX", typeof(double), typeof(UserControl_EjeX),  new FrameworkPropertyMetadata(5.0, new PropertyChangedCallback(OnDoubleChanged)));
        public double NroDivX
            get { return (double)GetValue(NroDivXProperty); }
            set { SetValue(NroDivXProperty, value); }

        public static DependencyProperty GrosorLineaProperty = DependencyProperty.Register("GrosorLinea", typeof(double), typeof(UserControl_EjeX), new FrameworkPropertyMetadata(1.0, new PropertyChangedCallback(OnDoubleChanged)));
        public double GrosorLinea
            get { return (double)GetValue(GrosorLineaProperty); }
            set { SetValue(GrosorLineaProperty, value); }

        public static DependencyProperty ColorNumerosProperty = DependencyProperty.Register("ColorNumeros", typeof(Brush), typeof(UserControl_EjeX),new FrameworkPropertyMetadata(Brushes.DarkOliveGreen, new PropertyChangedCallback(OnColorChanged)));
        public Brush ColorNumeros
            get { return (Brush)GetValue(ColorNumerosProperty); }
            set { SetValue(ColorNumerosProperty, value); }

        private static void OnColorChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
            UserControl_EjeX GrapCont = (UserControl_EjeX)sender;
            GrapCont.aColorNumeros = (Brush)e.NewValue; 
        private static void OnDoubleChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
            UserControl_EjeX GrapCont = (UserControl_EjeX)sender;
            if (e.Property == NroDivXProperty)
                GrapCont.aNroDivX = (double)e.NewValue; 
            else if (e.Property == GrosorLineaProperty)
                GrapCont.aGrosorLinea = (double)e.NewValue;
Main window not updated my data
<Window x:Class="WpfPruebaUserControl.MainWindow"
        Title="MainWindow" Height="300" Width="600">
        <local:UserControl_EjeX NroDivX="20" GrosorLinea="5" ColorNumeros="Green" />
Posted 10-Oct-12 7:43am
Edited 10-Oct-12 7:46am
Kenneth Haugland at 10-Oct-12 13:50pm
One comment:
public UserControl_EjeX()

DibujarEjeX (CanvasAreaDibujo);

What's going on here. Aren't you sending nothing as the new canvas? shouldn't it be included in the
public UserControl_EjeX(Canvas CanvasAreaDibujo) ?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

As far as I can tell, it looks like you have not redrawn the picture, just updated the property values for this class. Once you have the new values, you need to call the method (DibujarEjeX) that does the drawing, or you have to have references to the objects, and change thier properties. If you draw again, will want to clear the canvas.

DependencyProperties are intended to provide a way of communicating changes. You still need to do something with those changes. They are intended for Binding, and none of you GUI objects have any sort of Binding.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 999
1 Maciej Los 765
2 KrunalRohit 686
3 CPallini 606
4 Richard MacCutchan 430

Advertise | Privacy | Mobile
Web03 | 2.8.1509028.1 | Last Updated 10 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100