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

  • Home
  • SEARCH
  • 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 6225399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:50:09+00:00 2026-05-24T08:50:09+00:00

I’m a relative beginner with WPF so please bear with me. I have a

  • 0

I’m a relative beginner with WPF so please bear with me. I have a simple app that converts farenheit values to celcius and vice versa. I thought I would have a play with refactoring this to MVVM so I moved everything from my code-behind to a separate class and then set the dataContext programmatically. However I’m getting a lot of ..’does not exist in context errors’. Where am I going wrong? Thanks

XAML

<Window x:Class="FarenheitCelciusConverter.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Temperature Converter" Height="500" Width="500"
xmlns:local="clr-namespace:FarenheitCelciusConverter">
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Height="473" Width="488">

    <Label Height="28" HorizontalAlignment="Left" Margin="10,10,0,0" Name="lblF" VerticalAlignment="Top" Width="64" FontWeight="Bold">Farenheit</Label>
    <Label Height="28" HorizontalAlignment="Left" Margin="10,42,0,0" Name="lblC" VerticalAlignment="Top" Width="64" FontWeight="Bold">Celcius</Label>
    <TextBox Height="23" Margin="94,10,112,0" Name="tbFaren" VerticalAlignment="Top" Width="72" HorizontalAlignment="Left" />
    <TextBox Height="23" Margin="94,42,112,0" Name="tbCelcius" VerticalAlignment="Top" Width="72" HorizontalAlignment="Left" />
    <Button Margin="94,76,109,0" Name="btnConvert" Click="btnConvert_Click" Height="23" VerticalAlignment="Top" HorizontalContentAlignment="Center" Width="72" HorizontalAlignment="Left">Convert</Button>
    <Image Name="image1" Stretch="Fill" Margin="94,112,240,228">
        <Image.Source>
            <BitmapImage DecodePixelWidth="200" UriSource="C:\Users\Winston\Pictures\thermometer.jpg"/>
        </Image.Source>
    </Image>
    <TextBlock FontWeight="Bold" Height="21" Margin="195,12,173,0" Name="tblCelci" VerticalAlignment="Top" /><TextBlock FontWeight="Bold" Height="21" Margin="195,44,0,0" Name="tblFarenh" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120" /><TextBlock FontWeight="Bold" Height="21" Margin="195,78,15,0" Name="tblCex" VerticalAlignment="Top" Foreground="Red" />
</Grid>
</Window>

Code behind

namespace FarenheitCelciusConverter
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        DataContext = new ConverterViewModel();
    }   
}

}

View Model

namespace FarenheitCelciusConverter
{
    public class ConverterViewModel
    {        
    private void btnConvert_Click(object sender, RoutedEventArgs e)
    {
        tblCex.Text = "";

        try
        {
            if (tbCelcius.Text.Length != 0)
            {
                double celcius = Double.Parse(tbCelcius.Text);

                if (celcius < 99999.0 && celcius > -99999.0)
                {
                    tblFarenh.Text = Math.Round(1.8 * celcius + 32.0) + " F";
                }
                else
                {
                    throw new OverflowException("Number limit exceeded!");
                }
            }

            if (tbFaren.Text.Length != 0)
            {
                double farenh = Double.Parse(tbFaren.Text);

                if (farenh < 99999.0 && farenh > -99999.0)
                {
                    tblCelci.Text = Math.Round(0.555 * (farenh - 32.0)) + " C";
                }
                else
                {
                    throw new OverflowException("Number limit exceeded!");
                }
            }
        }

        catch (Exception ex)
        {
            tblCex.Text = ex.Message;
        }

    }  
}

}

  • 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-24T08:50:10+00:00Added an answer on May 24, 2026 at 8:50 am

    When using MVVM data is passed back and forth from the View (Window1) and the ViewModel through Databinding. So each of your textboxes should be databound to a public property in your Viewmodel:

    <TextBox Height="23" Margin="94,10,112,0" Name="tbFaren" VerticalAlignment="Top" Width="72" HorizontalAlignment="Left" Text="{Binding FahrenText}"/>
    

    And your ViewModel will take the values in the properties, do something with them, and set the properties bound to the appropriate textboxes.

    In this way, the Viewmodel is performing the logic and the View is interpreting the output according to the rules that you are giving it. Later on, you can change the rules in the View without messing with the ViewModel whereas using the codebehind often you have to explicitly set View settings alongside the program logic.

    Also, be sure to implement iNotifyPropertyChanged on your ViewModel , otherwise the UI wont know when the databound property values have changed and it wont update. Check out this post for an example.

    Also here is the MSDN article on Databinding in WPF.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
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 have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.