WPF ReadOnly Dependency Properties using MVVM

前端 未结 4 1877
心在旅途
心在旅途 2020-12-10 16:47

I\'ve recently overridden the DevXpress WPF grid to give myself a SelectedObject property that I can access from my loosely bound ViewModel.

I\'ve made a SelectedObj

4条回答
  •  误落风尘
    2020-12-10 17:08

    You're trying to set the SelectedObject property in the XAML. If it's read-only, how can you set it?

    Edit: sorry, my bad. Just realized what you're trying to do, and you're right that it should work. However, WPF doesn't support this scenario, at least in 3.5.

    Edit 2: Just checked in .NET 4 and same story.

    By the way, if you're stuck with someone else's readonly DP that you're trying to "push" into a VM, you can use an attached behavior to workaround this. For example, suppose you want your VM to be aware of the ActualWidth and ActualHeight properties of your view. You can write a SizeWatcherBehavior that attaches to the FrameworkElement and listens for size changes. When detected, those size changes are pushed to read/write attached properties that your VM can bind to:

    
    

提交回复
热议问题