WPF 因设置不期望的DataContext,导致的绑定异常
在MainWindow中,创建一个背景属性BrushTest,并将其绑定至界面上UserControl的BackgroundTest属性
<Window x:Class="WpfApp8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp8"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" x:Name="TheMainWindow">
<Grid>
<local:UserControl1 BackgroundTest="{Binding BrushTest}"/>
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
BrushTest = Brushes.Red;
this.DataContext = this;
}
public static readonly DependencyProperty BrushTestProperty = DependencyProperty.Register(
"BrushTest", typeof(SolidColorBrush), typeof(MainWindow), new PropertyMetadata(default(SolidColorBrush)));
public SolidColorBrush BrushTest
{
get { return (SolidColorBrush) GetValue(BrushTestProperty); }
set { SetValue(BrushTestProperty, value); }
}
}
UserControl,同样添加一个BackgroundTest属性,并将其绑定至界面。
<UserControl x:Class="WpfApp8.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp8"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid Background="{Binding BackgroundTest}">
</Grid>
</UserControl>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.DataContext = this;
}
public static readonly DependencyProperty BackgroundTestProperty = DependencyProperty.Register(
"BackgroundTest", typeof(SolidColorBrush), typeof(UserControl1), new PropertyMetadata(default(SolidColorBrush)));
public SolidColorBrush BackgroundTest
{
get { return (SolidColorBrush) GetValue(BackgroundTestProperty); }
set { SetValue(BackgroundTestProperty, value); }
}
}
运行后,控制台输出绑定异常,背景设置并没有生效。
System.Windows.Data Error: 40 : BindingExpression path error: 'BrushTest' property not found on 'object' ''UserControl1' (Name='')'.
BindingExpression:Path=BrushTest; DataItem='UserControl1' (Name=''); target element is 'UserControl1' (Name=''); target property is 'BackgroundTest' (type 'SolidColorBrush')
为何错了?
因为UserControl设置了俩次DataContext,UserControl1内部设置的上下文覆盖了主窗口设置的上下文。
窗口内<local:UserControl1 BackgroundTest="{Binding BrushTest}"/>绑定的值BrushTest,在UserControl下的上下文无法找到相关值,所以报错了
此类绑定异常,一不小心还是很容易出现的。
在窗口设置了DataContext时(自身或者ViewModel),子控件也设置DataContext。有趣的是,在Xaml编辑时,使用Reshaper链接到的是窗口所在的上下文属性。
所以,子控件设置DataContext时,需要关注下是否有属性被引用绑定外界数据。
建议子控件减少DataContext的使用,以上可以通过指定数据源进行绑定。比如:
<UserControl x:Class="WpfApp8.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp8"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" x:Name="TheUserControl">
<Grid Background="{Binding ElementName=TheUserControl,Path=BackgroundTest}">
</Grid>
</UserControl>
WPF 因设置不期望的DataContext,导致的绑定异常的更多相关文章
- WPF后台设置xaml控件的样式System.Windows.Style
WPF后台设置xaml控件的样式System.Windows.Style 摘-自 :感谢 作者: IT小兵 http://3w.suchso.com/projecteac-tual/wpf-zhi ...
- 备份数据库的时候设置 BufferCount 选项不正确导致 out of memory 的情况
备份数据库的时候设置 BufferCount 选项不正确导致 out of memory 的情况 今天群里面的东辉兄跟我说备份生产数据库的时候报错 环境: 32位的SQLSERVER2008 机器有1 ...
- 关于FusionCharts图表宽度width的设置问题导致图表显示异常的解决办法
关于FusionCharts图表宽度width的设置问题导致图表显示异常的解决办法 题设: 经常使用FusionCharts图表的朋友可能会遇到这个问题.就是在FusionCharts显示的时候有时候 ...
- WPF: 自动设置Owner的ShowDialog 适用于MVVM
原文:WPF: 自动设置Owner的ShowDialog 适用于MVVM 原文地址:http://www.mgenware.com/blog/?p=339 WPF中的Windows的ShowDialo ...
- WPF Style设置和模板化Template
WPF样式设置和模板化是一套功能(样式,模板,触发器和演示图版),可以为产品设置统一外观.类似于html的css,可以快速的设置一系列属性值到控件. 案例:ButtonStyle 这里创建了一个目标类 ...
- wpf datagrid设置右键菜单打开时选中项的背景色
原文:wpf datagrid设置右键菜单打开时选中项的背景色 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/huangli321456/artic ...
- WPF 字体设置
原文:WPF 字体设置 WPF 主界面 更换字体 可全局 但是有的时候有的窗体 字体还是没变 可以做全局样式 <Window x:Class="CLeopardTestWpf.Main ...
- WPF combobox设置默认选项不生效的问题
combobox 是常用的控件,当我们需要绑定设置默认选项时,往往会绑定 SelectedItem 属性去设置, 可是你会惊奇地发现SelectedItem的值绑定了, 它依旧是熟悉的模样 根据官方的 ...
- [典型漏洞分享]YS VTM模块存在格式化字符串漏洞,可导致VTM进程异常退出【高危】
YS VTM模块存在格式化字符串漏洞,可导致VTM进程异常退出[高危] 问题描述: YS VTM模块开放对外监听端口(8554和8664),此次使用sulley fuzzing框架对监听在8664端口 ...
随机推荐
- django-模板之自动转义autoescape(八)
index.html {{QQ}} views.py def index(request): context={ 'QQ':'<a href="http://www.qq.com&qu ...
- django-MVT设计模式
MVT:Models.Views.Templates Model:封装数据库,对数据库进行访问,对数据进行增删查改等. View:业务逻辑的一些操作. Templates:展示. 而MVC主要的流程如 ...
- vue进入新页面,与原页面滚动到相同高度的解决方案
可以在vue路由新增scrollBehavior,控制跳转页面高度 import Router from 'vue-router' new Router({ scrollBehavior (to, f ...
- 讲一讲快速学习WPF的思路
我不想浪费大家的时间,直接奔主题了. 首先大家要明白,WPF跟Winform的区别,优点,缺点. 首先入门来讲 Winform简单点,WPF会难一点.所以第一次接触C# 我推荐用Winform项目去学 ...
- vue学习笔记(一)入门
前言 随着前端不断的壮大,许多公司对于前端开发者的需求也越来越多了,作为一名优秀的前端工程师,如果连vue和react都不会的话,那真是out了,为什么那么说呢?这是我在招聘网站上截的一张图,十家公司 ...
- 监控tomcat,自动启动
tomcatID=`ps -ef |grep tomcat |grep -v 'grep'|awk '{print $2}'` tomcatCount=`ps -ef|grep tomcat |gr ...
- Kong07-自定义 Kong 插件
在进一步讨论之前,有必要简要说明 Kong 是如何构建的,特别是它是如何与 Nginx 集成的,以及 Lua 与它有什么关系. 在 Nginx 中,lua-nginx-module 模块支持 Lua ...
- [考试反思]0801NOIP模拟测试11
8月开门红. 放假回来果然像是神志不清一样. 但还是要接受这个事实. 嗯,说好听点,并列rank#7. 说难听点,垃圾rank#18. 都不用粘人名就知道我是哪一个吧... 因为图片不能太长,所以就不 ...
- MySQL集群搭建详解(三种结点分离)
本文将搭建一个最简化的MySQL Cluster系统,配置方法中的所有命令都是以root账户运行.这个MySQL Cluster包含一个管理结点.两个数据结点.两个SQL结点,这五个结点会分别安装在五 ...
- 单元测试JUnit案例
被测试模块 package packagedemo; public class Largest { public int minimal(int [] array1) { int index = 0 ...