在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,导致的绑定异常的更多相关文章

  1. WPF后台设置xaml控件的样式System.Windows.Style

    WPF后台设置xaml控件的样式System.Windows.Style 摘-自 :感谢 作者: IT小兵   http://3w.suchso.com/projecteac-tual/wpf-zhi ...

  2. 备份数据库的时候设置 BufferCount 选项不正确导致 out of memory 的情况

    备份数据库的时候设置 BufferCount 选项不正确导致 out of memory 的情况 今天群里面的东辉兄跟我说备份生产数据库的时候报错 环境: 32位的SQLSERVER2008 机器有1 ...

  3. 关于FusionCharts图表宽度width的设置问题导致图表显示异常的解决办法

    关于FusionCharts图表宽度width的设置问题导致图表显示异常的解决办法 题设: 经常使用FusionCharts图表的朋友可能会遇到这个问题.就是在FusionCharts显示的时候有时候 ...

  4. WPF: 自动设置Owner的ShowDialog 适用于MVVM

    原文:WPF: 自动设置Owner的ShowDialog 适用于MVVM 原文地址:http://www.mgenware.com/blog/?p=339 WPF中的Windows的ShowDialo ...

  5. WPF Style设置和模板化Template

    WPF样式设置和模板化是一套功能(样式,模板,触发器和演示图版),可以为产品设置统一外观.类似于html的css,可以快速的设置一系列属性值到控件. 案例:ButtonStyle 这里创建了一个目标类 ...

  6. wpf datagrid设置右键菜单打开时选中项的背景色

    原文:wpf datagrid设置右键菜单打开时选中项的背景色 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/huangli321456/artic ...

  7. WPF 字体设置

    原文:WPF 字体设置 WPF 主界面 更换字体 可全局 但是有的时候有的窗体 字体还是没变 可以做全局样式 <Window x:Class="CLeopardTestWpf.Main ...

  8. WPF combobox设置默认选项不生效的问题

    combobox 是常用的控件,当我们需要绑定设置默认选项时,往往会绑定 SelectedItem 属性去设置, 可是你会惊奇地发现SelectedItem的值绑定了, 它依旧是熟悉的模样 根据官方的 ...

  9. [典型漏洞分享]YS VTM模块存在格式化字符串漏洞,可导致VTM进程异常退出【高危】

    YS VTM模块存在格式化字符串漏洞,可导致VTM进程异常退出[高危] 问题描述: YS VTM模块开放对外监听端口(8554和8664),此次使用sulley fuzzing框架对监听在8664端口 ...

随机推荐

  1. Python 中用面向对象的思想处理网页翻页 (初级)

    第一种处理方法: class Pagenation(object): """ 处理分页相关的代码 """ def __init__(self ...

  2. 9、pytest -- 集成文档测试

    目录 1. 集成doctest模块 1.1. 通过指定文本文件的方式 1.2. 通过编写文档字符串的方式 1.3. 指定额外的选项 2. 失败时继续执行 3. 指定输出的格式 4. 文档测试中使用fi ...

  3. 11 一步一步Zabbix4.4.0系统教你实现sendEmail邮件报警

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 一步一步Zabbix4.4.0系统教你实现sendEmail邮件报警 sendEmail是一个轻量 ...

  4. Spring入门介绍

    概述 下载地址:https://repo.spring.io/release/org/springframework/spring/ spring是开源的轻量级框架 spring核心的主要两部分 AO ...

  5. 使用ESP8266 打造一款物联网产品---新版ESP8266-RTOS-SDK(V3.1以上)串口使用指南

    问题背景: 使用乐鑫的ESP8266做一个物联网的项目,要使用串口0通信,串口1作为打印log.本来是一个非常简单的事情.没想到居然里面有个大坑.本着前任踩坑,后任抱娃的原则. 这里就做个记录,给后面 ...

  6. [ES]Python查询ES导出数据为Excel

    版本 elasticsearch==5.5.0 python==3.7 说明 用python查询es上存储的状态数据,将查询到的数据用pandas处理成excel code # -*- coding: ...

  7. 看电影(movie):组合数

    Description 到了难得的假期,小白班上组织大家去看电影.但由于假期里看电影的人太多,很难做到让全班看上同一场电影,最后大家在一个偏僻的小胡同里找到了一家电影院.但这家电影院分配座位的方式很特 ...

  8. 智和网管平台SugarNMS助力网络安全运维等保2.0建设

    智和信通智和网管平台SugarNMS结合<信息安全技术 网络安全等级保护基本要求>(GB/T 22239-2019)等国家标准文件以及用户提出的网络安全管理需求进行产品设计,推出“监控+展 ...

  9. 2018年7月份前端开源软件TOP3

    基于 ThinkPHP5 + Bootstrap 的后台开发框架 FastAdmin FastAdmin 详细介绍 FastAdmin是一款基于 ThinkPHP5 + Bootstrap 的极速后台 ...

  10. python——时间模块

    格式化时间字符串 %y 两位数的年份表示(00-99) %Y 四位数的年份表示(0000-9999) %m 月份(01-12) %d 月内的一天(0-31) %H 24小时制的小时数(0-23) %I ...