I am a relative newbie with WPF, so please bear with me. I have a simple application that converts farenheit values ββto celcius and vice versa. I thought I would have a game with refactoring this in MVVM, so I moved everything from my code to a separate class, and then programmatically set the dataContext. However, I get a lot ... "does not exist in contextual 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 for
namespace FarenheitCelciusConverter {
}
Show 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; } } }
}