如何使用在一个类中声明的依赖项属性在wpf中将标签绑定到另一个类中(显示在另一个标签中) [英] How to use dependency property declared in one class to bind a label in another class in wpf (display in another label )

查看:131
本文介绍了如何使用在一个类中声明的依赖项属性在wpf中将标签绑定到另一个类中(显示在另一个标签中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类Radial.xaml.cs和ToothDimension.xaml.cs,想将Radial.xaml.cs文本框控件的值绑定到工作正常的另一个类ToothDimension.xaml.cs的依赖项属性.它没有绑定到文本框控件.做 我需要在Radial.xaml.cs中更改DataContext吗?我试过了:

I have two classes Radial.xaml.cs and ToothDimension.xaml.cs, want to bind value of the class Radial.xaml.cs textbox control to the dependency property of another class ToothDimension.xaml.cs which works fine. It's not getting bound to text box control. Do I need to change DataContext in Radial.xaml.cs? I tried this:

Radial.xaml.cs

public Radial()
    {
     InitializeComponent();
     DataContext = new ToothDimension(); 
    }

Radial.xaml

Radial.xaml

<Label x:Name="Length" Text="{Binding Path=ToothHeight}" HorizontalAlignment="Left" Height="35"/> 

ToothDimension.xaml.cs (在其中声明了ToothHeight)

ToothDimension.xaml.cs (in which ToothHeight is declared)

 public Double ToothHeight
    {
        get { return (double)GetValue(ToothHeightProperty); }
        set { SetValue(ToothHeightProperty, value); }
    }

 public static readonly DependencyProperty ToothHeightProperty 
     DependencyProperty.RegisterAttached("ToothHeight", 
     typeof(double), typeof(ToothDimensions), 
     new PropertyMetadata(new PropertyChangedCallback(ToothHeightChanged)));

private static void ToothHeightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        double value = (double)e.NewValue;
        ToothMeasurements thisControl = d as ToothMeasurements;
    }





推荐答案


user1888-13,


Hi user1888-13,

>>如何使用在一个类中声明的依赖项属性在wpf中将标签绑定到另一个类中

您的ToothHeight应该在DependencyObject类中定义:

Your ToothHeight should be defined in a DependencyObject Class:

    public class ToothHeightDOclass : DependencyObject
    {
        public static double GetToothHeight(DependencyObject obj)
        {
            return (double)obj.GetValue(ToothHeightProperty);
        }

        public static void SetToothHeight(DependencyObject obj, double value)
        {
            obj.SetValue(ToothHeightProperty, value);
        }

        public static readonly DependencyProperty ToothHeightProperty =
         DependencyProperty.RegisterAttached(
             "ToothHeight",
             typeof(double),
             typeof(ToothHeightDOclass),
             new PropertyMetadata(0.0, FinalValueChanged));

        private static void FinalValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            double newvalue = (double)e.NewValue;
            Label cell = obj as Label;
            if (newvalue >= 10)
            {
                cell.Content = newvalue.ToString();
                cell.Foreground = Brushes.DarkBlue;
            }
            else
            {
                cell.Content = newvalue.ToString();
                cell.Foreground = Brushes.Red;
            }
        }
    }

在XAML中使用:

    <Grid>
        <Label x:Name="Length" local:ToothHeightDOclass.ToothHeight="{Binding }"   HorizontalAlignment="Left" Height="35"/>
    </Grid>

最好的问候,

吕汉楠


这篇关于如何使用在一个类中声明的依赖项属性在wpf中将标签绑定到另一个类中(显示在另一个标签中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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