bind a value having dependency property from one class to another class textbox control in wpf

六月ゝ 毕业季﹏ 提交于 2019-12-12 03:29:35

问题


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

<TextBlock x:Name="Length" Text="{Binding DataContext.ToothHeight}" HorizontalAlignment="Left" Height="35"/> 

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

回答1:


Please change the path of your binding just to ToothHeight.

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

Though I would suggest you to use the MVVM pattern.




回答2:


It was binding correctly, I got it working .



来源:https://stackoverflow.com/questions/40795848/bind-a-value-having-dependency-property-from-one-class-to-another-class-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!