ReadOnly Dependency REF Properties Using MVVM

I recently redefined the WPF DevXpress grid to provide me with the SelectedObject property, with which I can access from my loosely coupled ViewModel.

I created the SelectedObject dependency property and bound it to OneWayToSource in my XAML.

Everthing works fine, but if I try to do this ReadOnly (for completeness), I get a compilation error and say that I cannot bind to the ReadOnly property. In the compilation code below, I turned on (but rem'd out) the bits that I tried in an attempt to get the ReadOnly property.

Can anyone help?

The dependency property of my overridden control looks like this:

//public static readonly DependencyPropertyKey SelectedRowKey = DependencyProperty.RegisterReadOnly("SelectedObject", typeof(object), typeof(MyGrid), new PropertyMetadata(null)); //public static readonly DependencyProperty SelectedObjectProperty = SelectedRowKey.DependencyProperty; public readonly static DependencyProperty SelectedObjectProperty = DependencyProperty.Register("SelectedObject", typeof(object), typeof(MyGrid), new PropertyMetadata(null)); public object SelectedObject { get { return GetValue(SelectedObjectProperty); } set { throw new NotImplementedException(); } } 

XAML:

  <StackPanel> <devxgrid:MyGrid AutoPopulateColumns="True" DataSource="{Binding Animals}" SelectedObject="{Binding MyObject, Mode=OneWayToSource}" Width="300" Height="300"> <devxgrid:MyGrid.View> <MyGrid:TableView AllowEditing="False" Name="GridView" AutoWidth="True" /> </devxgrid:MyGrid.View> </devxgrid:MyGrid> </StackPanel> 
+7
wpf mvvm binding dependency-properties
source share
3 answers

You are trying to set the SelectedObject property to XAML. If it is read-only, how can you install it?

Edit: sorry my bad. I just realized what you are trying to do, and you are right that it should work. However, WPF does not support this scenario, at least in 3.5.

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

By the way, if you are stuck with someone else just with DP, which you are trying to "paste" into a virtual machine, you can use the attached behavior to work around this. For example, suppose your virtual machine needs to know the ActualWidth and ActualHeight your view. You can write a SizeWatcherBehavior that attaches to the FrameworkElement and listens for resizing. Upon detection, these size changes are transferred to read / write attached properties that your virtual machine can associate with:

 <Grid local:SizeWatcherBehavior.Watch="True" local:SizeWatcherBehavior.Width="{Binding WidthOnVM, Mode=OneWayToSource}" local:SizeWatcherBehavior.Height="{Binding HeightOnVM, Mode=OneWayToSource}"/> 
+4
source share

I'm a little late for this question, given that it was asked almost 2 years ago :)

I made a decision to dynamically be able to redirect read-only dependency properties to a source called PushBinding , about which I wrote about it here . In your case, it will look like

 <devxgrid:MyGrid AutoPopulateColumns="True" DataSource="{Binding Animals}" Width="300" Height="300"> <pb:PushBindingManager.PushBindings> <pb:PushBinding TargetProperty="SelectedObject" Path="MyObject"/> </pb:PushBindingManager.PushBindings> <!--...--> </devxgrid:MyGrid> 

PushBinding works using two dependency properties, a listener and a mirror. The Listener is bound by OneWay to TargetProperty, and in PropertyChangedCallback it updates the Mirror property, which is bound by OneWayToSource to what was specified in Binding.

Demo project can be downloaded here .
It contains the source code and a brief use of the example, or visit my WPF blog if you are interested in implementation details.

+4
source share

We are developing a user management library, and one of our users requested a function to convert one of our software from read-only to read-write because it fell into the same problem as you - not can bind OneWayToSource into an MVVM script.

Of course, we did not do this DP read-write.

Using attached properties / behavior for such scenarios is a huge overhead. The simplest workaround would be to handle the SelectedObjectChanged event in the code behind and set the property that you wanted to bind to the "SelectedObject" DP in the code behind.

In our opinion, this โ€œevent call and call code from the VM / DataContext approach directlyโ€ does not violate MVVM in any way, since the ViewModel still does not know anything about the view.

0
source share

All Articles