WPF customization window data context

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 { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); DataContext = new ConverterViewModel(); } } 

}

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; } } } 

}

+4
source share
1 answer

When using data, MVVMs are passed back and forth from the view (Window1) and ViewModel through data binding. Thus, each of your text fields should be tied to a public property in the 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 values ​​in the properties, do something with them and set properties that are bound to the corresponding text fields.

That way, the Viewmodel executes the logic, and the view interprets the output according to the rules you give it. Later, you can change the rules in the view without interacting with the ViewModel, while when using the code, you often need to explicitly set the viewing parameters next to the program logic.

Also, be sure to implement iNotifyPropertyChanged on your ViewModel, otherwise the user interface will not know when the database property values ​​have changed and it will not be updated. An example is this post .

There is also an MSDN article on data binding in WPF .

+3
source