cesar salcedo

cesar salcedo

  • NA
  • 79
  • 1.6k

Dependency properties do not update my Graph User Control

Oct 10 2012 6:46 PM

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" />

    </Viewbox>

</UserControl>

 

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()

        {

            InitializeComponent();

            DibujarEjeX (CanvasAreaDibujo);

        }

 

        /* ********** Application Logic****************************************** */

 

        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),

            };

            CanvasAreaDibujo.Children.Add(EjeX);

 

 

            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 };

                CanvasAreaDibujo.Children.Add(tb);

                Canvas.SetLeft(tb, i - 2);

                Canvas.SetTop(tb, (MyCanvas.Height / 2.0f) + 10);

                k++;

            }

 

        }

 

        /* ****************  Dependency Properties  ************ */

 

        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); }

        }

 

        /* ***************** Callbacks Para Las Propiedades ****************** */

 

        private static void OnColorChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)

        {

            UserControl_EjeX GrapCont = (UserControl_EjeX)sender;

            GrapCont.aColorNumeros = (Brush)e.NewValue; // No se si sera : GrapCont.ColorNumeros = (Brush)e.NewValue;

        }

       

        private static void OnDoubleChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)

        {

            //UserControl_EjeX GrapCont = new UserControl_EjeX(); // En que casos se usa esta forma ? no lo se

            UserControl_EjeX GrapCont = (UserControl_EjeX)sender;

 

            if (e.Property == NroDivXProperty)

                GrapCont.aNroDivX = (double)e.NewValue; // o talvez sea  GrapCont.NroDivX = (double)e.NewValue; // no lo se

            else if (e.Property == GrosorLineaProperty)

                GrapCont.aGrosorLinea = (double)e.NewValue;

        }

 

    }

 

 

 

////////////////////////////////////////////////////////////////////////////////////////////////////

 

Main window not updated my data

 

<Window x:Class="WpfPruebaUserControl.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:local="clr-namespace:WpfPruebaUserControl"

        Title="MainWindow" Height="300" Width="600">

    <Grid>

        <local:UserControl_EjeX NroDivX="20" GrosorLinea="5" ColorNumeros="Green" />

    </Grid>

</Window>

 




i try

  public UserControl_EjeX()

        {

            InitializeComponent();

 

           // DibujarEjeX (CanvasAreaDibujo);

        }

 

        private static void OnDoubleChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)

        {

            UserControl_EjeX GrapCont = (UserControl_EjeX)sender;

           DibujarEjeX (CanvasAreaDibujo); // // I do not know

        }