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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:08:12+00:00 2026-05-23T13:08:12+00:00

I am learning wpf. I am trying to utilize tdd practices with wpf. I

  • 0

I am learning wpf. I am trying to utilize tdd practices with wpf. I have the following code in one of my viewmodels that saves data from a control into an xml file –

void saveStr()
        {
            string source = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Data\Connections.xml";

            DataGrid dg = FindChildViewItems.FindChild<DataGrid>(Application.Current.MainWindow, "MyDataGrid");
            List<XmlSettings> list = new List<XmlSettings>();

            foreach (var i in dg.Items)
            {
                if(i.GetType().ToString() == "MyProject.Configuration.XmlSettings")
                {
                    list.Add((XmlSettings)i);
                }
            }

            saveXml.Save(source, list);   
        }

The FindChildViewItems simply uses the visualtreehelper and dependency objects to find child items from the view (adapted from a post here).

My question is whether that adheres to tdd patterns since it has a dependency on the view when I am looking back to the view to get the contents of that particular control.

Is there another way to get the information from the datagrid on the view so that the contained data could be saved to an xml file.
I hope that makes sense……

Thanks for any thoughts.

  • 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-23T13:08:13+00:00Added an answer on May 23, 2026 at 1:08 pm

    Well, it looks like you’re breaking several patterns here. It’s definitely going to be very difficult to test.

    Perhaps I can help you smooth out your structure a bit to make it more testable.

    First, your viewmodel should not be saving state to a file! Your viewmodel should update the model whenever the user changes data in one of the datagrids, and nothing else. Use two-way databinding to allow your viewmodel to know when the view changes.

    Persisting state to a file should be done in a different part of the program, possibly by a standalone command object, possibly in an event (alot of where this should occur depends on your program structure, and I can only guess at that). Regardless of where it occurs, it should be outside both your viewmodel and your model, and it should use your model to obtain the data it writes to a file.

    After some minor tweaks to your method to account for this, it should be totally testable. Give it a stubbed model object (preferrably a class that only has user config settings) and let it go to town with your fake data.

    Testing writing the file then becomes your biggest issue. Difficult to automate.

    But testable.

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

Sidebar

Related Questions

I am currently learning WPF (C#) and have taken the following simple data binding
I'm in the process of learning WPF coming from WinForms development. I have a
I'm learning WPF and have been trying to create a toolstrip. Is there a
I'm learning WPF and MVVM and trying to stress good design. I have lots
While learning c# and wpf, i am trying to get grasp of data binding.
I have recently started learning wpf and am trying to use mvvm. My understanding
I've been learning wpf for about a week now.. and i have a basic
I just start learning WPF, is that possible I can create an IE toolbar
I am learning WPF and am trying to create my first UserControl. My UserControl
I'm still learning the WPF ropes, so if the following question is trivial or

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.