簡體   English   中英

WPF設置窗口數據上下文

[英]WPF Setting Window Data Context

我是WPF的初學者,所以請耐心等待。 我有一個簡單的應用程序,將farenheit值轉換為celcius,反之亦然。 我以為我會把這個重構到MVVM,所以我將所有內容從我的代碼隱藏到一個單獨的類,然后以編程方式設置dataContext。 但是我得到了很多......'在上下文錯誤中不存在'。 我哪里錯了? 謝謝

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>

代碼背后

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

}

查看模型

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

    }  
}

}

使用MVVM時,數據從View(Window1)和ViewModel通過Databinding來回傳遞。 因此,您的每個文本框都應該數據綁定到Viewmodel中的公共屬性:

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

並且您的ViewModel將獲取屬性中的值,對它們執行某些操作,並設置綁定到相應文本框的屬性。

通過這種方式,Viewmodel正在執行邏輯,View正在根據您提供的規則解釋輸出。 稍后,您可以在不破壞ViewModel的情況下更改View中的規則,而使用代碼隱藏通常需要在程序邏輯旁邊顯式設置View設置。

此外,請務必在ViewModel上實現iNotifyPropertyChanged,否則UI不會知道數據綁定屬性值何時更改且不會更新。 查看此帖子以獲取示例。

這里還有關於WPF中數據綁定的MSDN文章。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM