WPF如何绑定控件自身的属性

qq_34716866 2016-04-19 03:26:47
自定义控件LinkButton有一个依赖属性 Title,如何使自定义控件内部的Label控件的Content值绑定到自身的Title属性上呢?
也就是使title的Content值始终为控件自身的Title值。

我知道可以通过代码实现这样的效果,只是想确定一下,能否在XAML中达到目的


<UserControl x:Class="PartyNetwork.LinkButton"
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:PartyNetwork"
mc:Ignorable="d"
d:DesignHeight="66" d:DesignWidth="104" >

<Grid>
<Label x:Name="title" />
</Grid>

</UserControl>
...全文
1803 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
exception92 2016-04-19
  • 打赏
  • 举报
回复
后台 构造函数中设置 this.DataContext = this; 前台 <Label x:Name="title" Context="{Binding Title}" />
Forty2 2016-04-19
  • 打赏
  • 举报
回复 1
<Label Content="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=Title}" />

110,536

社区成员

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

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

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