开发者

WPF Setting Window Data Context

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" />
开发者_如何学Go    <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;
        }

    }  
}

}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜