问题
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