将具有依赖性 属性 的值从一个 class 绑定到 wpf 中的另一个 class 文本框控件
bind a value having dependency property from one class to another class textbox control in wpf
我有两个 classes Radial.xaml.cs 和 ToothDimension.xaml.cs,想要将 class Radial.xaml.cs 文本框控件的值绑定到依赖项 属性 另一个 class ToothDimension.xaml.cs 效果很好。它不受文本框控件的约束。我需要更改 Radial.xaml.cs 中的 DataContext 吗?我试过这个:
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(其中声明了 ToothHeight)
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;
}
请将您的绑定路径更改为ToothHeight
。
<TextBlock x:Name="Length" Text="{Binding ToothHeight}" HorizontalAlignment="Left" Height="35"/>
尽管我建议您使用 MVVM 模式。
它绑定正确,我让它工作了。
我有两个 classes Radial.xaml.cs 和 ToothDimension.xaml.cs,想要将 class Radial.xaml.cs 文本框控件的值绑定到依赖项 属性 另一个 class ToothDimension.xaml.cs 效果很好。它不受文本框控件的约束。我需要更改 Radial.xaml.cs 中的 DataContext 吗?我试过这个:
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(其中声明了 ToothHeight)
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;
}
请将您的绑定路径更改为ToothHeight
。
<TextBlock x:Name="Length" Text="{Binding ToothHeight}" HorizontalAlignment="Left" Height="35"/>
尽管我建议您使用 MVVM 模式。
它绑定正确,我让它工作了。