WPF,请看下这个UserControl

都放 2013-11-20 09:56:52

<UserControl x:Class="WPF1.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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button Height="30" Width="50" Background="{Binding Foreground, ElementName=this}"/>
</Grid>
</UserControl>



<Window x:Class="WPF1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF1"
Title="MainWindow" Height="450" Width="925" Background="#FFEFDB96">
<Grid>
<local:UserControl1 HorizontalAlignment="Left" Foreground="Red" Height="142" Margin="202,132,0,0" VerticalAlignment="Top" Width="234"/>
</Grid>
</Window>


UserControl中,有一个Button,其Background绑定到控件的Foreground属性上,使用的时候,设置了Foreground为红色,可是Button为什么没有呈现红色呢?
...全文
98 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cheng2005 2013-11-20
  • 打赏
  • 举报
回复
ElementName=this 可能我水平不够,WPF搞了这个长时间没见过这种用法
qldsrx 2013-11-20
  • 打赏
  • 举报
回复
UserControl-->Grid-->Button 你设置的是UserControl的Foreground属性,而不是Button的Foreground属性,看清楚可视化树的结构。
都放 2013-11-20
  • 打赏
  • 举报
回复
有人知道吗??
Teln_小凯 2013-11-20
  • 打赏
  • 举报
回复
Teln_小凯 2013-11-20
  • 打赏
  • 举报
回复
Background="{Binding Foreground}
Teln_小凯 2013-11-20
  • 打赏
  • 举报
回复
Element不要 UC里面加 <UserControl .... DataContext="{Binding RelativeSource={RelativeSource Self}}"/>
傀儡师苏摩 2013-11-20
  • 打赏
  • 举报
回复
2楼正解

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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