DependencyProperty how to?
本文关键字:to how DependencyProperty | 更新日期: 2023-09-27 18:27:19
我的问题是什么?它不起作用(mvvm)
我得到了空的文本框,当我更改文本框时,CurrentReadingChanged上的文本不被称为
namespace Test
{
public partial class MainWindow : Window
{
public MainWindow()
{
DataContext = new BaseModel();
this.InitializeComponent();
}
public static DependencyProperty ImgPositionProperty = DependencyProperty.Register("ImgPosition", typeof(string), typeof(MainWindow),
new PropertyMetadata("ddd", new PropertyChangedCallback(OnCurrentReadingChanged)));
private static void OnCurrentReadingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MessageBox.Show("1");
}
}
}
<Window xmlns:View="clr-namespace:Test">
<Grid x:Name="LayoutRoot">
<TextBox Margin="141,81,254,0" TextWrapping="Wrap" Text="{Binding View:MainWindow.ImgPosition, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top"/>
</Grid>
</Window>
如果您绑定到MainWindow
上的DependencyProperty
而不是您的DataContext
,则可以使用"FindAncestor"绑定
<TextBox Text="{Binding ImgPosition, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type View:MainWindow}}, UpdateSourceTrigger=PropertyChanged}" />