如何解决两个依赖属性已被更改而不是一个
我正在创建基于边框和标签的用户控件。我想将 UserControl 的 Content 属性设置为 Label 的 Content 属性。
有一个UserControl的WPF代码:
<UserControl x:Class="TestApp.TabButton"
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:TestApp"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Border x:Name="TabButtonBorder">
<Label x:Name="TabButtonLabel" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" Content="test content"/>
</Border>
还有一个类代码:
public partial class TabButton : UserControl
{
public TabButton()
{
InitializeComponent();
TabButtonLabel.DataContext = this;
DependencyPropertyDescriptor contentDescriptor = DependencyPropertyDescriptor.FromProperty(ContentProperty,typeof(TabButton));
contentDescriptor.AddValueChanged(TabButtonLabel,(s,e) => { ((Label)s).Content = Content; });
}
}
它以我想要的方式工作,但在标签的内容更新后前景变为黑色,但正如您在代码中看到的那样,它应该是白色的。为什么会这样?
解决方法
不确定您到底要达到什么目的,但是始终使用 Label
内的 Border
来可视化其 Content
的 UserControl 应该通过其 {{1 }}。不需要额外的代码。
ControlTemplate
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。