Wpf 如何设置标签的文本

Wpf 如何设置标签的文本,wpf,user-controls,dependency-properties,Wpf,User Controls,Dependency Properties,我有一个usercontrol,我想把它放在一个FixedDocument中,但是在我这样做之前,我需要更改标签的文本。我想我需要使用依赖属性 这里是简化的XAML <UserControl x:Class="PrinterTest.TestControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.m

我有一个usercontrol,我想把它放在一个FixedDocument中,但是在我这样做之前,我需要更改标签的文本。我想我需要使用依赖属性

这里是简化的XAML

<UserControl x:Class="PrinterTest.TestControl"
             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"
             DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <Label Content="{Binding LabelCaption}"
               Height="24" HorizontalContentAlignment="Right" Name="lblCaption"     
               Width="140" />
    </Grid>
</UserControl>
在调用位中,我通过
TestControl myControl=newtestcontrol()实例化


我做错了什么,因为我无法访问控件的新副本中的属性?谢谢大家!

LabelCaptionDP
更改为
LabelCaptionProperty

发件人:

财产及其支持的命名约定 DependencyProperty字段很重要。字段的名称始终为 属性的名称,并附加后缀属性。更多 有关此约定及其原因的信息,请参见自定义 依赖属性


请阅读中的依赖项属性名称约定。

感谢LPL-对命名约定进行了更改。在调用代码中,我尝试了UserControl TestControl=new TestControl();(TestControl)TestControl.LabelCaptionProperty=“Test”;但是我得到一个System.Windows.Controls.UserControl不包含LabelCaptionProperty的定义,并且我在Intellisense中看不到任何内容。我在做什么蠢事?谢谢LPL-更改了命名规则。在调用代码中,我尝试了UserControl TestControl=new TestControl();(TestControl)TestControl.LabelCaptionProperty=“Test”;但是我得到一个System.Windows.Controls.UserControl不包含LabelCaptionProperty的定义,并且我在Intellisense中看不到任何内容。我在做什么蠢事?为此,请使用Back CLR属性:
((TestControl)TestControl.LabelCaption=“Test”。谢谢。语法排序!这是我第一篇关于WPF乐趣的文章。我敢说,一旦我的课本到了,我可能会掌握窍门。现在我所要做的就是与我的网格轮廓搏斗。再次感谢你解除了我的沮丧。
public partial class TestControl : UserControl
{
    public TestControl()
    {
        InitializeComponent();
    }

    public readonly static DependencyProperty 
        LabelCaptionDP = DependencyProperty.Register("LabelCaption",
                                                     typeof(string), 
                                                     typeof(TestControl),
                                                     new FrameworkPropertyMetadata("no data"));

    public string LabelCaption
    {
        get { return (string)GetValue(LabelCaptionDP); }
        set { SetValue(LabelCaptionDP, value); }
    }