Skip to main content
 首页 » 编程设计

c#之将 ViewModel 直接绑定(bind)到扩展的 WPF 工具包 PropertyGrid

2024年02月13日11mfryf

我想将 ViewModel 直接绑定(bind)到 PropertyGrid,而 ViewModel 不必包含对自身的引用。即目前我有:

<xctk:PropertyGrid SelectedObject="{Binding ViewModel}"> 

在我的 ViewModel 中:

public class WatcherBaseViewModel : ObservableObject  
{ 
    [Browsable(false)] 
    // Do I really need this? 
    public WatcherBaseViewModel ViewModel 
    { 
        get { return this; } 
    } 
 
    // Rest of class removed for clarity 
 } 

因为这是有效的,所以它非常学术,但简洁是机智的灵魂......有什么方法可以直接在 XAML 绑定(bind)中引用 ViewModel?

请您参考如下方法:

删除 Path 表单绑定(bind),因为它将指向 DataContext,而它已经是 WatcherBaseViewModel:

<xctk:PropertyGrid SelectedObject="{Binding}">