c# - Can't read read-only property even with OneWay mode set -
viewmodel
namespace my.viewmodels { public class itemviewmodel : observableobject { private itemmodel _model; public itemviewmodel(itemmodel model) { _model = model; } public string name { { return _model.name; } } } }
xaml
<usercontrol x:class="my.controls.itemcontrol" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:viewmodels="clr-namespace:my.viewmodels" mc:ignorable="d" d:designheight="421" d:designwidth="786" d:datacontext="{d:designinstance viewmodels:itemviewmodel}"> <grid background="white"> <textblock><run text="name:" /> <run text="{binding name, fallbackvalue=name, mode=oneway}" /></textblock> </grid> </usercontrol>
error:
a twoway or onewaytosource binding cannot work on read-only property 'name'
i'm trying databinding read-only property viewmodel. i've set binding mode oneway.. yet still throws error above. i'm out of clues! appreciated.
to use oneway binding property should has , set. in case, fix problem add set property this:
public string name { private set; { return _model.name; }
Comments
Post a Comment