首页 / C# / c# – WPF设置窗口数据上下文
c# – WPF设置窗口数据上下文
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – WPF设置窗口数据上下文,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3566字,纯文字阅读大概需要6分钟。
内容图文
![c# – WPF设置窗口数据上下文](/upload/InfoBanner/zyjiaocheng/784/70282288c18341e69711c188098e6f43.jpg)
我是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不会知道数据绑定属性值何时更改且不会更新.查看this post的示例.
这也是Databinding in WPF上的MSDN文章.
内容总结
以上是互联网集市为您收集整理的c# – WPF设置窗口数据上下文全部内容,希望文章能够帮你解决c# – WPF设置窗口数据上下文所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。