c# - WPF with MVVM and DataAnnotations, Validation Errors in a UserControl -
i have usercontrol reused throughout application developing. using framework based on mvvmlight.
for sake of simplicity lets user control contains 1 textbox , exposes 1 dependency property named "quantity". textbox on user control databound dependency property "quantity".
when user control used on view, "quantity" dependency property of usercontrol databound property in viewmodel. (this viewmodel datacontext of our view way of mvvmlight viewmodellocator).
this works great! bindings work, properties set expect. until comes validation.
we using dataannotations decorate our viewmodel properties. viewmodel contains custom implementation of inotifydataerrorinfo. have implemented custom styles input controls show red border around control, , message next control displaying validation error message. of works great in normal case (eg. textbox on view bound property in view model).
when attempt same approach using user control, end red border around entire user control , no error indication on actual textbox. appears fact there error being reflected in ui, it's not making control want to.
i've searched on stackoverflow problem, of questions solutions, none seem work situation.
my first guess because actual textbox bound directly dependency property , not property on view model, not being notified of errors generated. there way propogate errors generated in viewmodel through usercontrol , textbox?
any or suggestions can give great, thanks.
here usercontrol xaml.
<usercontrol x:class="sampleproject.usercontrols.samplecontrol" 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" mc:ignorable="d" x:name="samplecontrol" d:designheight="300" d:designwidth="300"> <grid x:name="layoutroot" datacontext="{binding elementname=samplecontrol}"> <textbox text="{binding path=quantity, validatesondataerrors=true}" width="100" height="30" /> </grid> </usercontrol>
the usercontrol code behind.
public partial class samplecontrol : usercontrol { public samplecontrol() { initializecomponent(); } public static readonly dependencyproperty quantityproperty = dependencyproperty.register("quantity", typeof(int?), typeof(samplecontrol), new frameworkpropertymetadata{defaultvalue=null, bindstwowaybydefault = true}); public int? quantity { { return (int?)getvalue(quantityproperty); } set { setvalue(quantityproperty, value); } } }
used on view.
<usercontrols:samplecontrol grid.row="1" quantity="{binding path=quantity, validatesondataerrors=true}" height="60" width="300"/>
the viewmodel property.
[required(errormessage = "is required")] [range(5, 10, errormessage = "must greater 5")] public int? quantity { { return _quantity; } set { set(() => quantity, ref _quantity, value); } } private int? _quantity;
(*note, set method in setter helper method in base viewmodel sets backing property , raises propertychanged event it.)
try removing datacontext
usercontrol
. instead of setting that, bind
directly textbox
actual property using relativesource
binding
:
<textbox text="{binding quantity, relativesource={relativesource mode=findancestor, ancestortype={x:type yourcontrolnamespace:samplecontrol, validatesondataerrors=true}}}" width="100" height="30" />
update >>>
failing that, long view models bound property always have property of same name bind to, can binding
search through parents' datacontext
s this:
<textbox text="{binding quantity, relativesource={relativesource mode=findancestor, ancestorlevel=2, validatesondataerrors=true}}}" width="100" height="30" />
you need change 2
correct number of parent elements textbox
has before reaching control access correct property. example, using level of 2
means framework try find property named quantity
bind
in datacontext
of textbox
s parent's parent control. is trickier getting working ancestorlevel
though believe 'hidden' elements grid
s not included parents.
Comments
Post a Comment