c# - Getting lookup view model to trigger Modal Dialog -



c# - Getting lookup view model to trigger Modal Dialog -

what's right way viewmodel trigger custom lookup command throw modal dialog represents lookup viewmodel? custom lookup control's info context of parent record view model. lookup command has dependencyproperty has bound lookupviewmodel property on parent record view model , represents sub lookupviewmodel.

method 1) currrently utilize event on lookupviewmodel custom command knows hear for.

method 2) tried throwing validation exception within setter of property on lookupviewmodel lookup control's text propery bound too. hooked errorevent in custom lookup control. seems if user "corrects" value within dialog while in event, original value sticks. , worse, after phone call validation.clearinvalid, errorevent still fires somehow adds error back. works here in sense viewmodels have right data, it's seems textbox ignoring bound text property has changed on underlying info source when within errorevent. seems can't right error while within processing of error?

another sub issue within method 2 validation.clearinvalid doesn't remove reddish error border. had manually clear errortemplate too. right?

i'd find way utilize natural error handling within command throw modal dialog.

this isn't utilize events for. events exist facilitate decoupling: object raising event shouldn't know or care object(s) listening doing. you're expecting event able alter value of property within property's setter - or worse, event handler calling property setter that's raising event it's handling, means have pretty hackish avoid stack overflow.

your description isn't clear (you're describing both problem you're having , non-working solutions you're trying @ same time, confusing), sounds you're trying more like:

if (isvalid(value)) { _property = value; } else { _property = getvaluefromdialog(); }

the problem don't want have code in view model throws dialog, since creates view model can't tested outside of wpf application.

the reply in case utilize dependency injection. create interface called idialogservice:

interface idialogservice { object getvaluefromdialog(); }

now add together property view model:

public idialogservice dialogservice { get; set; }

the above code becomes:

if (isvalid(value)) { _property = value; } else { _property = dialogservice.getvaluefromdialog(); }

create dialog service utilize in wpf application throws dialog , gets result. when instantiate view model in application, this:

myviewmodel vm = new myviewmodel { dialogservice = new wpfdialogservice(); }

thus, in application, property setter set dialog , result expect to.

for unit tests, create mock dialog looks this:

public class mockdialogservice : idialogservice { private object result; public mockdialogservice(object result) { result = result; } public object getvaluefromdialog() { homecoming result; } }

you can write test like:

myviewmodel vm = new myviewmodel { dialogservice = mockdialogservice(expectedresult) }; vm.property = invalidvalue; assert.areequal(expectedresult, vm.property);

the above more sketch of solution solution - depending on how application uses dialogs, may need lot more features sketched out here. if take @ mvvm frameworks you'll find lot of them implement dialog services of 1 kind or another.

c# wpf mvvm binding

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -