绑定到模板parent';WPF中有谁的名字?

绑定到模板parent';WPF中有谁的名字?,wpf,data-binding,custom-controls,Wpf,Data Binding,Custom Controls,我正在尝试创建一个WPF自定义控件,它将自身绑定到模板化父级的x:Name属性。我认为应该这样做的代码也是由Visual Studio中的Binding Maker生成的,如下所示: Binding RelativeSource={RelativeSource TemplatedParent}, Path=Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay 但这不会产生任何结果。我可以用任何纯文本和所需的行为替换绑定(当且仅当用户未在控

我正在尝试创建一个WPF自定义控件,它将自身绑定到模板化父级的x:Name属性。我认为应该这样做的代码也是由Visual Studio中的Binding Maker生成的,如下所示:

Binding RelativeSource={RelativeSource TemplatedParent}, Path=Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay
但这不会产生任何结果。我可以用任何纯文本和所需的行为替换绑定(当且仅当用户未在控件中键入任何内容时,文本才会覆盖在控件上),但我的目标是将其作为一种工具提示,以便用户知道字段应该是什么(如字段的x:Name属性中定义的)

以下是我的完整xaml:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SuperTB">
<Style TargetType="{x:Type local:SuperTextB}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:SuperTextB}">
                <Border Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                    <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, Mode=TwoWay, UpdateSourceTrigger=LostFocus}">
                        <TextBox.Style>
                            <Style TargetType="TextBox">
                                <Style.Triggers>
                                    <Trigger Property="Text" Value="">
                                        <Setter Property="Background">
                                            <Setter.Value>
                                                <VisualBrush Stretch="None">
                                                    <VisualBrush.Visual>
                                                        <TextBlock Foreground="Gray" FontSize="24" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsRequired}"></TextBlock>
                                                    </VisualBrush.Visual>
                                                </VisualBrush>
                                            </Setter.Value>
                                        </Setter>
                                    </Trigger>
                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsRequired}" Value="True">
                                        <Setter Property="BorderThickness" Value="4" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBox.Style>
                    </TextBox>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

作为旁注,双向绑定到
Name
属性对我来说通常是个坏主意,因为该属性用于范围界定、引用和绑定。这很有意义,我将关闭它。谢谢你的问题令人困惑。您在第一部分中提到了
x:Name
,但在您的控件中根本不引用它。另外,您的控件有一个MyName属性,在XAML中似乎根本没有使用该属性。到底是什么不起作用?在我的应用程序中,正在对此类自定义控件设置x:Name属性。我的目标是将x:Name属性绑定到控件中VisualBrush的Textblock,这样我就可以在xaml中将其中一个命名为类似FirstName的名称,然后在用户输入文本之前,控件会说FirstName。我试着毫无乐趣地绑定到MyName属性(它目前硬编码到unicorns,因此我知道它在调试期间被设置为某个值)。希望这能澄清我的问题?但在完整的XAML中,您需要绑定到
IsRequired
。我猜你只是在测试东西,然后发布了错误的东西。你的意思是
Name
MyName
,对吗?
    public class SuperTextB : Control
{

    static SuperTextB()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(SuperTextB), new FrameworkPropertyMetadata(typeof(SuperTextB)));

    }

    public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(SuperTextB));

    public string Text
    {
        get { return (String)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }


    private static DependencyProperty myNameProperty =
DependencyProperty.Register("MyName", typeof(string), typeof(SuperTextB), new PropertyMetadata("Unicorns !", NameChanged));

    private static void NameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

    }

    public string MyName
    {
        get { return (string)GetValue(myNameProperty); }
        set { SetValue(myNameProperty, value); }
    }
    DependencyProperty isRequiredProperty =
        DependencyProperty.Register("IsRequired", typeof(bool), typeof(SuperTextB), new PropertyMetadata(false, IsReqChanged));

    private static void IsReqChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

    }

    public bool IsRequired
    {
        get { return (bool)GetValue(isRequiredProperty); }
        set { SetValue(isRequiredProperty, value); }
    }
}