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 976359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:44:06+00:00 2026-05-16T03:44:06+00:00

I have two instances of a File Browser user control, the control is just

  • 0

I have two instances of a File Browser user control, the control is just a button and a text box, but I need two instances, think a diff utility. Once the user has selected a file for each control I want to enable a button which will perform an action on both of the files.

The problem I am having is how to distinguish between the instances of the controls in order to determine that both files have been selected. I think I would like for my DoSumthinViewModel to only have string properties which the FileChooserViewModel fulfills.

At first I had a single ViewModelLocator with a property which returned a new instance of a the FileChooserVM when accessed, but this just didn’t seem right and I could not distinguish between the instances. I then went down the path of a separate Locator for the FileChooser but realized that each control would be talking to the same locator instance and thus the same FileChooserViewModel again.

So, what would be a good technique for working with individual instances of the same ViewModel?

Thanks,

Shane Holder

  • 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-16T03:44:06+00:00Added an answer on May 16, 2026 at 3:44 am

    Your DoSomethingViewModel could have two properties of type FileChooserViewModel that your controls are bound to, then check their string properties for a value.

    A simplified version of your FileChooserViewModel could be…

    public class FileChooserViewModel : ViewModelBase
    {
        public const string FilePathPropertyName = "FilePath";
        private string _filePath;
        public string FilePath
        {
            get { return _filePath; }
            set
            {
                if (_filePath == value) return;
                _filePath = value;
                RaisePropertyChanged(FilePathPropertyName);
                Messenger.Default.Send(new NotificationMessage("FilePath Updated"));
            }
        }
    }
    

    And your DoSomethingViewModel might look like this…

    public class DoSomethingViewModel : ViewModelBase
    {
        public DoSomethingViewModel()
        {
            Messenger.Default.Register<NotificationMessage>(this, NotificationMessageReceived);
        }
    
        public const string FileChooser1PropertyName = "FileChooser1";
        private FileChooserViewModel _fileChooser1 = new FileChooserViewModel();
        public FileChooserViewModel FileChooser1
        {
            get { return _fileChooser1; }
            set
            {
                if (_fileChooser1 == value) return;
                _fileChooser1 = value;
                RaisePropertyChanged(FileChooser1PropertyName);
            }
        }
    
        public const string FileChooser2PropertyName = "FileChooser2";
        private FileChooserViewModel _fileChooser2 = new FileChooserViewModel();
        public FileChooserViewModel FileChooser2
        {
            get { return _fileChooser2; }
            set
            {
                if (_fileChooser2 == value) return;
                _fileChooser2 = value;
                RaisePropertyChanged(FileChooser2PropertyName);
            }
        }
    
        public const string BothFilesChosenPropertyName = "BothFilesChosen";
        public bool BothFilesChosen
        {
            get
            {
                var result = false;
                if (FileChooser1 != null && FileChooser2 != null)
                    result = !string.IsNullOrWhiteSpace(FileChooser1.FilePath)
                          && !string.IsNullOrWhiteSpace(FileChooser2.FilePath);
                return result;
            }
        }
    
        private void NotificationMessageReceived(NotificationMessage msg)
        {
            if (msg.Sender is FileChooserViewModel)
                RaisePropertyChanged(BothFilesChosenPropertyName);
        }
    }
    

    The NotificationMessageReceived method is called with a NotificationMessage is sent from FileChooserViewModel’s FilePath property setter, and it in turn raises the property changed event on the BothFilesChosen property.

    <UserControl x:Class="DoSomethingProject.Views.DoSomethingView"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:views="clr-namespace:DoSomethingProject.Views"
                 DataContext="{Binding DoSomethingViewModel, Source={StaticResource Locator}}">
        <StackPanel>
            <views:FileChooser DataContext="{Binding Path=FileChooser1}" />
            <views:FileChooser DataContext="{Binding Path=FileChooser2}" />
            <Button IsEnabled="{Binding Path=BothFilesChosen}" />
        </StackPanel>
    </UserControl>
    

    Another way to do this would be to handle the PropertyChanged event on each FileChooserViewModel property, but I prefer using messaging because event handling means you need to make sure you un-handle the events, and this can get messy leading to memory issues when events are missed.

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

Sidebar

Related Questions

I have two separate instances of Vim running. I would like to set up
I am developing a Java application that is to have two web interfaces: a
i have just started using servlet. basically i am new to web projects. I
I am creating a mobile version of my website. It is nothing fancy, just
Consider I have a Singleton class defined as follows. public class MySingleton implements Serializable{
This may be a question for ServerFault but I was more interested in understanding
A client has a pdf newsletter that weighs in at 1.63 MB, so not
I am having an infuriating experience with IIS7, Python 2.6, Mercurial 1.7.2, and hgweb.cgi.
Read through this code. I wrote it at my desk in 5 minutes, it's
We are evaluating the use of Logback in a multi-server Weblogic environment. On one

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.