I'm trying to bind to a Readonly
property with OneWayToSource
as mode, but it seems this cannot be done in XAML:
<controls:FlagThingy IsModified="{Binding FlagIsModified,
ElementName=container,
Mode=OneWayToSource}" />
I get:
The property 'FlagThingy.IsModified' cannot be set because it does not have an accessible set accessor.
IsModified
is a readonly DependencyProperty
on FlagThingy
. I want to bind that value to the FlagIsModified
property on the container.
To be clear:
FlagThingy.IsModified --> container.FlagIsModified
------ READONLY ----- ----- READWRITE --------
Is this possible using just XAML?
Update: Well, I fixed this case by setting the binding on the container and not on the FlagThingy
. But I'd still like to know if this is possible.
This is a limitation of WPF and it is by design. It is reported on Connect here:
OneWayToSource binding from a readonly dependency property
I made a solution to dynamically be able to push read-only dependency properties to the source called
PushBinding
which I blogged about here. The example below doesOneWayToSource
Bindings from the read-only DP'sActualWidth
andActualHeight
to the Width and Height properties of theDataContext
PushBinding
works by using two Dependency Properties, Listener and Mirror. Listener is boundOneWay
to the TargetProperty and in thePropertyChangedCallback
it updates the Mirror property which is boundOneWayToSource
to whatever was specified in the Binding.Demo Project can be Downloaded Here.
It contains source code and short sample usage, or visit my WPF blog if you're interested in the implementation details.
Wrote this:
Usage:
Code:
Have not tested it in styles and templates yet, guess it needs special casing.
WPF will not use the CLR property setter, but seems it does some odd validation based on it.
May be in your situation this can be ok:
Some research results for OneWayToSource...
Option # 1.
Option # 2
Option # 3 (True read-only dependency property)
System.ArgumentException: 'IsModified' property cannot be data-bound.
Reflector gives the answer:
You're doing the binding in the wrong direction right now. OneWayToSource will try and update FlagIsModified on container whenever IsModified changes on the control you are creating. You want the opposite, which is to have IsModified bind to container.FlagIsModified. For that you should use the binding mode OneWay
Full list of enumeration members: http://msdn.microsoft.com/en-us/library/system.windows.data.bindingmode.aspx
Here is another implementation for binding to Validation.HasError
Usage in xaml
This implementation is specific to binding
Validation.HasError