为什么没显示

weikeni19 2017-05-05 02:59:08
请问Id和Name属性为什么没显示 不是说向根查找吗?
XAML代码如下:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="135" Width="500">
<StackPanel Background="LightBlue">
<StackPanel.DataContext>
<local:Student Id="6" Age="29" Name="Tim" />

</StackPanel.DataContext>
<StackPanel>

<Grid>
<Grid.DataContext>
<local:Liweike Book="深入浅出"/>
</Grid.DataContext>
<StackPanel>

<TextBox Text="{Binding Path=Id}"/>
<TextBox Text="{Binding Path=Book}"/>
<TextBox Text="{Binding Name}"/>
</StackPanel>
</Grid>
</StackPanel>

</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 WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
public class Liweike
{
public string Book { get; set; }
}
}
...全文
127 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
weikeni19 2017-05-05
  • 打赏
  • 举报
回复
引用 5 楼 duanzi_peng 的回复:
[quote=引用 4 楼 weikeni19 的回复:] [quote=引用 3 楼 duanzi_peng 的回复:] <TextBox Text="{Binding Path=Id}"/> <TextBox Text="{Binding Path=Book}"/> <TextBox Text="{Binding Name}"/> -》改为:

 <TextBox Text="{Binding Path=DataContext.Id,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=StackPanel,AncestorLevel=2}}"/>
<TextBox Text="{Binding Path=Book}"/>
<TextBox Text="{Binding DataContext.Name,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=StackPanel,AncestorLevel=2}}"/>
如果不懂RelativeSource、FindAncestor,AncestorType,AncestorLevel的使用,网上查询用法,很容易理解
那为什么我的方法不行呢 根据定义 是向根方向的元素逐个查找[/quote] 因为Grid设置了 DataContext,这个就是textbox的“根元素”, 默认的一旦查找到包含DataContext的元素 就不会再向上查找。[/quote] 一次性用具?
exception92 2017-05-05
  • 打赏
  • 举报
回复
引用 4 楼 weikeni19 的回复:
[quote=引用 3 楼 duanzi_peng 的回复:] <TextBox Text="{Binding Path=Id}"/> <TextBox Text="{Binding Path=Book}"/> <TextBox Text="{Binding Name}"/> -》改为:

 <TextBox Text="{Binding Path=DataContext.Id,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=StackPanel,AncestorLevel=2}}"/>
<TextBox Text="{Binding Path=Book}"/>
<TextBox Text="{Binding DataContext.Name,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=StackPanel,AncestorLevel=2}}"/>
如果不懂RelativeSource、FindAncestor,AncestorType,AncestorLevel的使用,网上查询用法,很容易理解
那为什么我的方法不行呢 根据定义 是向根方向的元素逐个查找[/quote] 因为Grid设置了 DataContext,这个就是textbox的“根元素”, 默认的一旦查找到包含DataContext的元素 就不会再向上查找。
weikeni19 2017-05-05
  • 打赏
  • 举报
回复
引用 3 楼 duanzi_peng 的回复:
<TextBox Text="{Binding Path=Id}"/> <TextBox Text="{Binding Path=Book}"/> <TextBox Text="{Binding Name}"/> -》改为:

 <TextBox Text="{Binding Path=DataContext.Id,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=StackPanel,AncestorLevel=2}}"/>
<TextBox Text="{Binding Path=Book}"/>
<TextBox Text="{Binding DataContext.Name,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=StackPanel,AncestorLevel=2}}"/>
如果不懂RelativeSource、FindAncestor,AncestorType,AncestorLevel的使用,网上查询用法,很容易理解
那为什么我的方法不行呢 根据定义 是向根方向的元素逐个查找
exception92 2017-05-05
  • 打赏
  • 举报
回复
<TextBox Text="{Binding Path=Id}"/> <TextBox Text="{Binding Path=Book}"/> <TextBox Text="{Binding Name}"/> -》改为:

 <TextBox Text="{Binding Path=DataContext.Id,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=StackPanel,AncestorLevel=2}}"/>
<TextBox Text="{Binding Path=Book}"/>
<TextBox Text="{Binding DataContext.Name,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=StackPanel,AncestorLevel=2}}"/>
如果不懂RelativeSource、FindAncestor,AncestorType,AncestorLevel的使用,网上查询用法,很容易理解
weikeni19 2017-05-05
  • 打赏
  • 举报
回复
引用 1 楼 Libby1984 的回复:
DataContext向根查找,直到遇到有设置DataContext的容器。 你代码中包含TextBlocok的StackPanel没有设置DataContext,于是就向上查找,当找到上一个Grid的时候,反向已经设置了DataContext,所以不会再继续向上查找了。向根查找,是指向那个方向,并不是一定要找根节点为止,万一根节点没设置呢?
什么意思 什么叫反向已经设置了DataContext,我各种情况都试过了 都不管用 怎么回事 你帮我改改
  • 打赏
  • 举报
回复
DataContext向根查找,直到遇到有设置DataContext的容器。 你代码中包含TextBlocok的StackPanel没有设置DataContext,于是就向上查找,当找到上一个Grid的时候,反向已经设置了DataContext,所以不会再继续向上查找了。向根查找,是指向那个方向,并不是一定要找根节点为止,万一根节点没设置呢?

8,737

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧