C# 将自定义控件内的TextBlock绑定到同一自定义控件的依赖项属性

C# 将自定义控件内的TextBlock绑定到同一自定义控件的依赖项属性,c#,wpf,data-binding,xaml,custom-controls,C#,Wpf,Data Binding,Xaml,Custom Controls,我有一个自定义控件,其中包含一个文本块: <Style TargetType="{x:Type local:CustControl}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:CustControl}"> <Border Back

我有一个自定义控件,其中包含一个文本块:

<Style TargetType="{x:Type local:CustControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustControl}">
                <Border Background="Blue"
                        Height="26" 
                        Width="26" Margin="1">

                        <TextBlock x:Name="PART_CustNo"
                                   FontSize="10"
                                   Text="{Binding Source=CustControl,Path=CustNo}" 
                                   Background="PaleGreen" 
                                   Height="24" 
                                   Width="24"
                                   Foreground="Black">
                        </TextBlock>

                </Border>
             </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
我希望在自定义控件的每个实例中,在TextBlock的“Text”属性中传递“CustNo”属性的值。 但是我的:

它不起作用了

不适用于Path=CustNoProperty:

Text="{Binding Source=CustControl,Path=CustNoProperty}"
试试这个问题的答案。我想你会想要第三个例子。即:

{Binding Path=CustNo, RelativeSource={RelativeSource TemplatedParent}}

你需要一个模板绑定,比如

<TextBlock
   Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CustNo}" />


ps在Simeon有一个关于创建自定义控件的精彩(如果有点过时的话)dnrtv节目,谢谢。我只需要你的回答。很抱歉,无法选择几个完全相同的正确答案,这些答案是在两个被接受的同时发布的。我的答案是在Ian的答案发布前2分钟发布的,但你必须在发布时注意到。没有压力。也许下次。。。
{Binding Path=CustNo, RelativeSource={RelativeSource TemplatedParent}}
<TextBlock
   Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CustNo}" />