Click here to Skip to main content
12,072,640 members (63,768 online)
Rate this:
 
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" />
    </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);
        }
 

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

        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"
        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>
Posted 10-Oct-12 8:43am
Edited 10-Oct-12 8:46am
v2
Comments
Kenneth Haugland 10-Oct-12 13:50pm
   
One comment:
public UserControl_EjeX()
{
InitializeComponent();

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
 
good
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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160208.1 | Last Updated 10 Oct 2012
Copyright © CodeProject, 1999-2016
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