【发布时间】:2011-07-25 05:44:03
【问题描述】:
我有很多现有的业务对象,其中有很多属性和集合,我想在其中绑定用户界面。在这些对象中使用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.
}
}
<Grid Name="test">
<TextBlock Text="{Binding Path=artikel.MyProperty}" />
</Grid>
这是,我试图将我的对象打包到一个 DependencyProperty 中并尝试对此调用 UpdateTarget,但没有成功。
如何更新相应的 UI 控件?
我希望我描述的情况足够好。
【问题讨论】:
标签: c# wpf xaml data-binding dependency-properties