MTR*_*MTR 0 c# data-binding wpf xaml dependency-properties
我有很多现有的业务对象,其中包含许多属性和集合,我想在其中绑定用户接口.使用DependencyProperty
或ObservableCollections
在这些对象中使用不是一种选择.据我所知,当我修改这些对象时,我希望有一种机制来在我执行此操作时更新所有UI控件.另外,我也不知道哪些UI控件绑定到这些对象以及哪些属性.
这是我现在尝试做的简化代码:
public class Artikel
{
public int MyProperty {get;set;}
}
public partial class MainWindow : Window
{
public Artikel artikel
{
get { return (Artikel)GetValue(artikelProperty); }
set { SetValue(artikelProperty, value); }
}
public static readonly DependencyProperty artikelProperty =
DependencyProperty.Register("artikel", typeof(Artikel), typeof(MainWindow), new UIPropertyMetadata(new Artikel()));
public MainWindow()
{
InitializeComponent();
test.DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
artikel.MyProperty += 1;
// What can I do at this point to update all bindings?
// What I know at this point is that control test or some of it's
// child controls bind to some property of artikel.
}
}
Run Code Online (Sandbox Code Playgroud)
<Grid Name="test">
<TextBlock Text="{Binding Path=artikel.MyProperty}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
这是,我试图将我的对象打包到DependencyProperty并尝试在此调用UpdateTarget,但没有成功.
我该怎么做才能更新相应的UI控件?
我希望我描述的情况足够好.
使用INotifyPropertyChanged
是DependencyProperties的一个很好的替代品.
如果实现了接口,则可以PropertyChanged
使用null
as参数引发事件,以通知UI所有属性都已更改.