依赖项属性不会更新我的“图形用户控件" [英] Dependency properties do not update my Graph User Control

查看:90
本文介绍了依赖项属性不会更新我的“图形用户控件"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,向大家致以最诚挚的祝愿,您能为我提供帮助,解决这一小问题将为我提供更多帮助,请参见:
我有一个用户控件,其中有一个模仿X轴的小水平线图
在它的下面,画出它们的数字,很简单.
在尝试从主窗口通过XAML更改该控件的属性时,无法更新图形和显示新值,但是在我的依赖项属性中没有出现故障,请帮忙.
该代码可读性强,请耐心等待.

 <   UserControl     x:Class   ="  WpfPruebaUserControl.UserControl_EjeX"  
 
                     span>           等等   > 
    
    <   Viewbox     ="  填充" <  画布    ="   200" 宽度   400"   保证金  ="  2" 名称   CanvasAreaDibujo" / > 
    <  /Viewbox  > 
<  /UserControl  >  


 公共 部分  class  UserControl_EjeX:UserControl
    {
         double  aNroDivX = 10d;
         double  aSepMarcasX;
         double  aGrosorLinea = 2d;
        刷aColorNumeros =  SolidColorBrush(Color.FromRgb( 238  201  0 ));

        公共 UserControl_EjeX()
        {
            InitializeComponent();

            DibujarEjeX(CanvasAreaDibujo);
        }


        公共 无效 DibujarEjeX(Canvas MyCanvas)
        {
            Line EjeX =  Line()
            {
                描边=  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 =  TextBlock(){Text =(k).ToString(),Foreground = aColorNumeros,FontFamily =  FontFamily(" ),FontSize =  9 ,FontWeight = FontWeights.Bold};
                CanvasAreaDibujo.Children.Add(tb);
                Canvas.SetLeft(tb,i- 2 );
                Canvas.SetTop(tb,(MyCanvas.Height/ 2  .0f)+  10 ));
                k ++;
            }
        }


        公共 静态 DependencyProperty NroDivXProperty = DependencyProperty.Register("  NroDivX" double  ),类型(UserControl_EjeX), FrameworkPropertyMetadata( 5 . 0  PropertyChangedCallback(OnDoubleChanged)));
        公共  double  NroDivX
        {
            获取 {返回( double )GetValue( NroDivXProperty); }
             set  {SetValue(NroDivXProperty, value ); }
        }


        公共 静态 DependencyProperty GrosorLineaProperty = DependencyProperty.Register("  GrosorLinea" double  ),类型(UserControl_EjeX), FrameworkPropertyMetadata( 1 . 0  PropertyChangedCallback(OnDoubleChanged)));
        公共  GrosorLinea
        {
            获取 {返回( double )GetValue( GrosorLineaProperty); }
             set  {SetValue(GrosorLineaProperty, value ); }
        }


        公共 静态 DependencyProperty ColorNumerosProperty = DependencyProperty.Register("  ColorNumeros" typeof (UserControl_EjeX), FrameworkPropertyMetadata(Brushes.DarkOliveGreen, PropertyChangedCallback(OnColorChanged)));
        公共画笔颜色
        {
            获取 {返回(画笔)GetValue(ColorNumerosProperty); }
             set  {SetValue(ColorNumerosProperty, value ); }
        }



        私有 静态  void  OnColorChanged(DependencyObject发送者, DependencyPropertyChangedEventArgs e)
        {
            UserControl_EjeX GrapCont =(UserControl_EjeX)sender;
            GrapCont.aColorNumeros =(Brush)e.NewValue;
        }
        

        私有 静态  void  OnDoubleChanged(DependencyObject发送者, DependencyPropertyChangedEventArgs e)
        {
            UserControl_EjeX GrapCont =(UserControl_EjeX)sender;

            如果(例如,Property == NroDivXProperty)
                GrapCont.aNroDivX =( double )e.NewValue;
            其他 如果(例如,Property == GrosorLineaProperty)
                GrapCont.aGrosorLinea =( double )e.NewValue;
        }

    } 


主窗口未更新我的数据

 <   Window     x:Class   ="  
 
                     span>    xmlns   ="  http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                     span>    xmlns:x   ="  http://schemas.microsoft.com/winfx/2006/xaml" 
                     span>    xmlns:local   ="  clr-namespace:WpfPruebaUserControl" 
                     span>   标题  ="  MainWindow" 高度   300"  Width   ="  >> 
    <  网格 > 
        <   local:UserControl_EjeX     NroDivX   ="   GrosorLinea    5 "  ColorNumeros   绿色" / <  /Grid  > 
<  /Window  >  

DependencyProperties旨在提供一种传达变更的方式.您仍然需要对这些更改进行处理.它们用于绑定,并且您的GUI对象都没有任何绑定.


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>

解决方案

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.


这篇关于依赖项属性不会更新我的“图形用户控件"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆