在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. vue cli3.3 以上版本配置vue.config.js 及反向代理操作解决跨域操作

    const webpack = require('webpack') module.exports = { configureWebpack: { plugins: [ new webpack.Pro ...

  2. Spring Boot项目在多环境下(开发、生产或测试环境)调用不同配置文件方式

    写在前面 最近由于项目要求,原先的项目只有开发环境的项目配置,后来不利于线上测试,于是,最近对于SpringBoot这部分多环境配置在网上查找了相关资料,并实现了配置,于是为了防止遗忘,特在此进行总结 ...

  3. deepin15.5 安装tensorflow-gpu

    deepin的CUDA和cuDNN安装方法与其它系统有所不同,参考其它操作系统的方法也许不适用,特别是显卡驱动的安装,容易使系统出现问题 本次配置: 操作系统:deepin15.5桌面版 电脑品牌:联 ...

  4. quartus使用串口IP模块

    在quartus平台中使用串口模块的IP,需要使用到platform designer软件来实现. 1.在quartus界面调出IP Catalog界面. 2.在IP catalog中搜索UART,找 ...

  5. 大家都说好用的 Python 命令行库:click

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

  6. C++学习笔记11_STL

    STL又叫标准模板库,提供各种容器. STL是C++一部分,不休要额外安装什么,它被内建在编译器之内. STL重要特点是,数据结构和实现分离. *所谓迭代器,类似一个游标,使用++指向下一个元素,使用 ...

  7. 【控制系统数字仿真与CAD】实验二:结构图法数字仿真

    一. 实验目的 1. 掌握结构图法仿真复杂控制系统的方法: 2. 掌握复杂系统联接矩阵W和输入联接矩阵W0的求解过程: 3. 掌握复杂系统的环节连接,矩阵A. B. C.D的求解过程: 4. 掌握MA ...

  8. SSHD服务安全的连接

    SSHD服务 SSH 安全的远程连接 OpenSSH 工具 centos服务端的包:openssh-server centos客户端的包:openssh-clients 主要配置文件一般安装完成后再/ ...

  9. Mybatis自定义TypeHandler解决特殊类型转换问题

    我们知道,Java和MySQL中的数据类型是不同的,Java中除了基本数据类型,还有对象. 有时候使用MySQL存储数据,或者从MySQL中读取数据时,会有一些特殊需求

  10. jquery手指触摸滑动放大图片的方法(比较靠谱的方法)

    jquery手指触摸滑动放大图片的方法(比较靠谱的方法) <pre><!DOCTYPE html><html lang="zh-cn">< ...