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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:13:37+00:00 2026-06-17T16:13:37+00:00

I’ve started an MVVM project and now I’m stucking with correct DataBinding. My project

  • 0

I’ve started an MVVM project and now I’m stucking with correct DataBinding.
My project has:

A UserControl whit a ViewModel as DataContext like:

public partial class TestUserControl: UserControl
{
   public TestUserControl()
   {
       this.DataContext = new TestUserControlViewModel();
   }
}

ViewModel code is (BaseViewModel class contains PropertyChangedEventHandler):

public class TestUserControlViewModel : BaseViewModel
{
   public KrankenkasseControlViewModel()
   {}

   public IEnumerable<DataItem> GetAllData
   {
      get
      {
          IGetTheData src= new DataRepository();
          return src.GetData();
      }
   }
}

IGetTheData is the interface to DataContext:

public interface IGetTheData
{
   IEnumerable<DataItem> GetData();
}

}

and finally the DataRepository code:

public class DataRepository : IGetTheData
{
   private TestProjectDataContext dax = new TestProjectDataContext();

   public IEnumerable<DataItem> GetData()
   {
       return (from d in this.dax.TestData
               select new DataItem
               {
                  ID = d.ID,
                  SomeOtherData = d.SomeOtherData
               });
   }
}

My UserControl has a few TextBoxes, but what’s the best way to bind correctly?

Thanks for your help, regards.

  • 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-06-17T16:13:37+00:00Added an answer on June 17, 2026 at 4:13 pm

    EDIT: Binding the data against multiple textboxes

    After reading your comment, I will elaborate my example for textboxes.

    First important thing is that the ViewModel will model the things in the View, so that the View gets all information it needs in the structure it needs. That means, if you have multiple textboses in the View, you will need multiple string Properties in your ViewModel, one for each textbox.

    In your XAML you could have something like

    <TextBox Text="{Binding ID, Mode=TwoWay}" />
    <TextBox Text="{Binding SomeOtherData, Mode=TwoWay}" />
    

    and in your ViewModel

    public class TestUserControlViewModel : BaseViewModel {
        private string id;
        private string someOtherData;
    
        public TestUserControlViewModel() {
            DataItem firstItem = new DataRepository().GetData().First();
            this.ID = firstItem.ID;
            this.SomeOtherData = firstItem.SomeOtherData;
        }
    
        public string ID {
            get {
                return this.id;
            }
            set {
                if (this.id == value) return;
                this.id = value;
                this.OnPropertyChangedEvent("ID");
            }
        }
    
        public string SomeOtherData {
            get {
                return this.someOtherData;
            }
            set {
                if (this.someOtherData == value) return;
                this.someOtherData = value;
                this.OnPropertyChangedEvent("SomeOtherData");
            }
        }
    }
    

    Here I assume that in your BaseViewModel there is an OnPropertyChangedEvent method to fire the corresponding event. This tells the View that the property has changed and it must update itself.

    Note the Mode=TwoWay in the XAML. This means, that it doesn’t matter on which side the value changes, the other side will reflect the change immediately. So if the user changes a value in a TwoWay bound TextBox, then the corresponding ViewModel property will automatically change! And also vice versa: if you change the ViewModel property programmatically, the View will refresh.

    If you want to show multiple textboxes for more than one data item, then you must introduce more Properties in the ViewModel and bind them accordingly. Maybe a ListBox with a flexible number of TextBoxes inside is a solution then, like @Haspemulator already answered.

    Binding the data against a collection control

    In the TestUserControl I guess you have a control (like a ListView) to show the list of loaded things. So bind that control against the list in the ViewModel with

    <ListView ... ItemsSource="{Binding GetAllData}" ... />
    

    First you must understand that Binding means not “read the data and then forget the ViewModel”. Instead you bind the View to the ViewModel (and its Properties) as long as the View lasts. From this point of view, AllData is a much better name than GetAllData (thanks @Malcolm O’Hare).

    Now in your code, every time the View reads the AllData property, a new DataRepository is created. Because of the Binding, that is not what you want, instead you want to have one instance of DataRepository for the whole lifetime of the View, which is used to read the initial data and can later be used to update the View, if the underlying database changes (maybe with an event).

    To enable such a behavior you should change the type of the AllData property to an ObservableCollection, so that the View can automatically update the list if changes occur.

    public class TestUserControlViewModel : BaseViewModel
        private ObservableCollection<DataItem> allData;
    
        public TestUserControlViewModel() {
             IGetTheData src = new DataRepository();
             this.allData = new ObservableCollection<DataItem>(src.GetData());
        }
    
        public ObservableCollection<DataItem> AllData {
            get {
                return this.allData;
            }
        }
    
        public void AddDataItem(DataItem item) {
            this.allData.Add(item);
        }
    }
    

    Now if you call AddDataItem later, the ListView will update itself automatically.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
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
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:

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.