Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6149907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:31:59+00:00 2026-05-23T19:31:59+00:00

What’s the right way to get my viewmodel to trigger a custom lookup control

  • 0

What’s the right way to get my viewmodel to trigger a custom lookup control to throw up a modal dialog that essentially represents that lookup viewmodel? The custom lookup control’s data context is that of the parent record view model. The lookup control also has another DependencyProperty that has it bound to a lookupviewmodel property on the parent record view model and this represents a sub lookupviewmodel.

METHOD 1) I currrently use an event on the lookupviewmodel that the custom control knows to listen for.

METHOD 2) I tried throwing a validation exception within the setter of the property on the lookupviewmodel that the lookup control’s text propery is bound too. Then I hooked the ErrorEvent in the custom lookup control. But it seems that if the user “corrects” the value from within the dialog while in this event, the original value sticks. And worse, even after I call Validation.ClearInvalid, another ErrorEvent still fires that somehow adds the error back. So everything works here in the sense that all the viewmodels have the correct data, it’s just that it seems like the textbox is ignoring that the bound text property has changed on the underlying data source when inside an ErrorEvent. So it seems like I can’t correct an error while inside the processing of that error?

Another sub issue within method 2 is that Validation.ClearInvalid doesn’t remove the red error border. I had to manually clear the ErrorTemplate too. Is that right?

I’d like to find a way to use natural error handling within the control to get it to throw up the modal dialog.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:31:59+00:00Added an answer on May 23, 2026 at 7:31 pm

    This isn’t what you use events for. Events exist to facilitate decoupling: the object raising the event shouldn’t know or care what the object(s) listening to it are doing. You’re expecting an event to be able to change the value of a property from inside the property’s setter – or worse, your event handler is calling the very property setter that’s raising the event that it’s handling, which means that you have to do something pretty hackish to avoid a stack overflow.

    Your description isn’t very clear (you’re describing both the problem you’re having and the non-working solutions you’re trying at the same time, which is confusing), but it sounds like what you’re trying to do is something more like:

    if (IsValid(value))
    {
       _Property = value;
    }
    else
    {
       _Property = GetValueFromDialog();
    }
    

    The problem is that you don’t want to have code in your view model that throws up a dialog, since that creates a view model that can’t be tested outside of your WPF application.

    The answer in this case is to use dependency injection. Create an interface called IDialogService:

    interface IDialogService
    {
       object GetValueFromDialog();
    }
    

    Now add this property to your view model:

    public IDialogService DialogService { get; set; }
    

    The above code becomes:

    if (IsValid(value))
    {
       _Property = value;
    }
    else
    {
       _Property = DialogService.GetValueFromDialog();
    }
    

    Create a dialog service for use in your WPF application that actually throws up the dialog and gets the result. When you instantiate your view model in your application, do this:

    MyViewModel vm = new MyViewModel { DialogService = new WpfDialogService(); }
    

    Thus, in your application, the property setter will put up the dialog and get the result exactly as you expect it to.

    For your unit tests, create a mock dialog that looks like this:

    public class MockDialogService : IDialogService
    {
       private object Result;
    
       public MockDialogService(object result)
       {
          Result = result;
       }
    
       public object GetValueFromDialog() { return Result; }
    }
    

    You can then write a test like:

    MyViewModel vm = new MyViewModel { DialogService = MockDialogService(ExpectedResult) };
    vm.Property = InvalidValue;
    Assert.AreEqual(ExpectedResult, vm.Property);
    

    The above is really more a sketch of a solution than a solution – depending on how your application uses dialogs, you may need a lot more features than what are sketched out here. If you take a look at MVVM frameworks you’ll find that a lot of them implement dialog services of one kind or another.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.