关于DataContext的
请问我书上的(2)怎么用?我自己写了一段代码 但没显示 该怎么改?或者你们写一段代码给我看看 谢谢了 高分献上
书上的内容:
我的代码是这样的
XAML代码是这样的 我在项目里新建了一个窗口Window1
<Window x:Class="WpfApplication5.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBox Text="{Binding Id}"/>
</Grid>
</Window>
--------------------------------------------------------------------------
MainWindow的Xaml代码是这样的:
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:WpfApplication5"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<StackPanel.DataContext>
<sys:Student Id="6"/>
</StackPanel.DataContext>
<Button Click="Button_Click_1"/>
</StackPanel>
</Window>
----------------------------------------------------------------------
C#后台代码是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication5
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Window1 a = new Window1();
a.ShowDialog();
}
}
public class Student
{
public int Id { get; set; }
}
}
书上的内容:

我的代码是这样的
XAML代码是这样的 我在项目里新建了一个窗口Window1
<Window x:Class="WpfApplication5.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBox Text="{Binding Id}"/>
</Grid>
</Window>
--------------------------------------------------------------------------
MainWindow的Xaml代码是这样的:
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:WpfApplication5"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<StackPanel.DataContext>
<sys:Student Id="6"/>
</StackPanel.DataContext>
<Button Click="Button_Click_1"/>
</StackPanel>
</Window>
----------------------------------------------------------------------
C#后台代码是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication5
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Window1 a = new Window1();
a.ShowDialog();
}
}
public class Student
{
public int Id { get; set; }
}
}
...全文
相关推荐
Linq DataContext
**事务支持**:DataContext支持事务,可以确保一组操作作为一个单元执行,要么全部成功,要么全部回滚。**DataContext的使用**1.
WPF中DataContext的绑定技巧
先看效果:上面的绑定值都是我们自定义的属性,有了以上的提示,那么我们可以轻松绑定字段,再也不用担心错误了。附带源码。
DataContext 数据绑定
当你在一个控件上设置DataContext后,它的所有子控件都将默认继承这个DataContext,除非子控件有自己独立的DataContext设定。
WPF数据绑定DataContext[项目源码]
DataContext是每个UI元素的默认数据源,它定义了元素绑定数据的上下文环境。当在WPF中创建用户界面时,开发者可以通过设置DataContext来指定控件应从哪个数据源获取数据。
DataContext 绑定 ViewModel
**DataContext的工作原理:**1.


